#include<bits/stdc++.h>
#define gc getchar
#define pc putchar
#define F(i,x,y) for(int i=x;i<=y;++i)
#define R(i,x,y) for(int i=x;i>=y;--i)
typedef long long LL;
inline int read(){
	int x=0;char ch=gc();bool p=1;
	while (!isdigit(ch)) ch=='-'?p=0:0,ch=gc();
	while ( isdigit(ch)) x=(x<<1)+(x<<3)+(ch^'0'),ch=gc();
	return p?x:-x;
}
const int N=1e3+5,MOD=998244353;
int n,m,cc,ff,A[N][N],f[N][N],g[N][N],sumf[N][N];
inline void WorkC(){
	int ans=0;
	F(j,1,m){
		int res=0,r=0;
		F(i,1,n){
			if (A[i][j]) continue;
			if (r>=i+1){
				res-=f[i+1][j];
			}
			while (r<g[i][j]+i){
				++r;
				if (r>=i+2) res+=f[r][j];
			}
			//printf("i = %d, j = %d, res = %d\n",i,j,res);
			ans=(ans+(LL)f[i][j]*res%MOD)%MOD;
		}
	}
	printf("%d ",cc*ans);
}
inline void WorkF(){
	int ans=0;
	F(j,1,m){
		F(i,1,n){
			sumf[i][j]=sumf[i-1][j]+f[i][j];
		}
	}
	F(j,1,m){
		int res=0,r=0;
		F(i,1,n){
			if (A[i][j]) continue;
			if (r>=i+1){
				res=(res-f[i+1][j]*(g[i][j]-1))%MOD;
			}
			while (r<g[i][j]+i){
				++r;
				if (r>=i+3) res=(res+sumf[r-1][j]-sumf[i+1][j])%MOD;
			}
			//printf("i = %d, j = %d, res = %d\n",i,j,res);
			ans=(ans+(LL)f[i][j]*res%MOD)%MOD;
		}
	}
	ans=(ans+MOD)%MOD;
	printf("%d\n",ff*ans);
}
inline bool get(){
	char ch=gc();
	while (ch!='0'&&ch!='1') ch=gc();
	return ch=='1';
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int t=read(),id=read();
	while (t--){
		n=read(),m=read(),cc=read(),ff=read();
		F(i,1,n) F(j,1,m) A[i][j]=get();
		F(i,1,n){
			int r=0;
			F(j,1,m){
				if (r<j) r=j;
				while (r<m&&!A[i][r]&&!A[i][r+1]) ++r;
				f[i][j]=r-j;
				if (f[i][j]<0) f[i][j]=0;
				//printf("f[%d][%d] = %d\n",i,j,f[i][j]);
			}
		}
		F(j,1,m){
			int r=0;
			F(i,1,n){
				if (r<i) r=i;
				while (r<n&&!A[r][j]&&!A[r+1][j]) ++r;
				g[i][j]=r-i;
				if (g[i][j]<0) g[i][j]=0;
				//printf("g[%d][%d] = %d\n",i,j,g[i][j]);
			}
		}
		WorkC();
		WorkF();
	}
	return 0;
}
