#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define long long long
const long mod=998244353;
void rd(int &x){
	x=0;char c=getchar();
	while(c>'9'||c<'0')c=getchar();
	while(c>='0'&&c<='9')x=x*10+(c^48),c=getchar();
}
char pc[1010][1010];long res1,res2;int T,id;
long n,m,c,f,s[1010][1010],t[1010][1010],dp[1010][1010],res[1010][1010];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	for(scanf("%d%d",&T,&id);T--;){
		memset(s,0,sizeof s);
		memset(t,0,sizeof t);
		memset(dp,0,sizeof dp);
		memset(res,0,sizeof res);
		res1=res2=0;
		scanf("%lld%lld%lld%lld",&n,&m,&c,&f);
		for(int i=1;i<=n;i++)scanf("%s",pc[i]+1);
		for(int i=1;i<=n;i++)for(int j=m;j;j--){
			if(pc[i][j]=='1')s[i][j]=0;
			else s[i][j]=s[i][j+1]+1;
		}
		for(int i=1;i<=m;i++)for(int j=n;j;j--){
			if(pc[j][i]=='1')t[j][i]=0;
			else t[j][i]=t[j+1][i]+1;
		}
		for(int i=3;i<=n;i++)for(int j=1;j<=m;j++)if(pc[i][j]!='1'&&pc[i-1][j]!='1'){
			dp[i][j]=dp[i-1][j];
			if(s[i-2][j]>1)dp[i][j]=(dp[i][j]+s[i-2][j]-1)%mod;
			if(s[i][j]>1)res[i][j]=dp[i][j]*(s[i][j]-1)%mod;
		}
		for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
			res1=(res1+res[i][j])%mod;
			if(t[i][j]>1)res2=(res2+res[i][j]*(t[i][j]-1))%mod;
		}
		cout<<res1*c<<" "<<res2*f<<endl;
	}
	return 0;
}
