#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5,mod=998244353;
int t,id,n,m,c,f,R[N][N],D[N][N],suf[N][N],sum[N][N],ac,af;
char s[N][N];
int qrysuf(int j,int l,int r){
	if(l>r) return 0;
	return (suf[j][l]-suf[j][r+1])%mod;
}
int qrysum(int j,int l,int r){
	if(l>r) return 0;
	return (sum[j][l]-sum[j][r+1])%mod;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&t,&id);
	while(t--){
		scanf("%d%d%d%d",&n,&m,&c,&f),ac=af=0;
		for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
		for(int i=1;i<=n;i++)
			for(int j=m;j>=1;j--)
				R[i][j]=s[i][j]=='0'?R[i][j+1]+1:0;
		for(int i=n;i>=1;i--)
			for(int j=1;j<=m;j++)
				D[i][j]=s[i][j]=='0'?D[i+1][j]+1:0;
		for(int j=1;j<=m;j++)
			for(int i=n;i>=1;i--)
				suf[j][i]=(suf[j][i+1]+R[i][j]-1)%mod,
				sum[j][i]=(sum[j][i+1]+1ll*(R[i][j]-1)*(D[i][j]-1)%mod)%mod;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				if(R[i][j]>=2&&D[i][j]>=3)
					ac=(ac+1ll*(R[i][j]-1)*qrysuf(j,i+2,i+D[i][j]-1)%mod)%mod,
					af=(af+1ll*(R[i][j]-1)*qrysum(j,i+2,i+D[i][j]-1)%mod)%mod;
		ac=(ac+mod)%mod,af=(af+mod)%mod;
		printf("%lld %lld\n",1ll*c*ac%mod,1ll*f*af%mod);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++) R[i][j]=D[i][j]=suf[i][j]=sum[i][j]=0;
	}
	return 0;
}