#include<bits/stdc++.h>
#define maxn 1005
#define int long long 
#define ull unsigned long long
#define put() putchar('\n')
using namespace std;
inline void read(int &x) {
	x=0;char ch=getchar();int f=1;
	while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	x*=f;
}
int a[maxn][maxn];
int n,m,cc,ff;
const int mod=998244353;
int s[maxn][maxn];
namespace Solve1{
	int ans;
	inline void main(void) {
		int i,j;ans=0;
		for (j=1;j<=m;j++) {
			int sum=0;
			for (i=n;i>=1;i--) {
				if (i+2<=n) {
					if (a[i+2][j]==0) sum=(sum+s[i+2][j])%mod;else sum=0;
				}
				if (i+1<=n) {
					if (a[i+1][j]==1) {sum=0;continue;}
				}
				if (a[i][j]==0) ans=(ans+sum*s[i][j])%mod;else sum=0;
			}
		}
		printf("%lld ",(cc*ans)%mod);
	}
}
namespace Solve2{
	int ans;
	inline void main(void) {
		int i,j;ans=0;
		for (j=1;j<=m;j++) {
			int sum1=0,sum2=0;
			for (i=n;i>=1;i--) {
				if (a[i][j]==0) sum1++;else {sum1=0,sum2=0;continue;}
				if (i+2<=n) {
					if (a[i+2][j]==0&&sum1>=3) sum2=(sum2+(s[i+2][j]*(sum1-3)))%mod;
					else sum2=0; 
 				}
 				if (i+1<=n) {
 					if (a[i+1][j]==1) {sum1=1;sum2=0;continue;}
				}
				ans=(ans+sum2*s[i][j])%mod;
			}
		} 
		printf("%lld",ff*ans%mod);
	}
}
inline void solve(void) {
	int i,j;char ch;
	read(n);read(m);read(cc);read(ff);
	for (i=1;i<=n+1;i++) for (j=1;j<=m+1;j++) a[i][j]=-1;
	for (i=1;i<=n;i++) {
		cin>>ch;
		for (j=1;j<=m;j++) a[i][j]=(ch=='1'),ch=getchar();
	}
	for (i=1;i<=n+2;i++) for (j=1;j<=m+2;j++) s[i][j]=0;
	for (i=1;i<=n;i++) {
		for (j=m;j>=1;j--)
			if (a[i][j]==0) {
				if (a[i][j+1]==0) s[i][j]=s[i][j+1]+1;
				else s[i][j]=0; 
			}
	}
	Solve1::main();
	Solve2::main();
	put();
}
signed main(void) {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	read(T);read(id);
	while (T--) solve(); 
	
	return 0;
}