#include <bits/stdc++.h>
using namespace std;
#define int long long //if TL,ML is tight!
char s[1010][1010];
int r[1010][1010];
int pre[1010];
const int M = 998244353;
int qpow(int x,int y) {
	int ans=1;
	while(y) {
		if(y&1) ans=ans*x%M;
		x=x*x%M;
		y>>=1;
	}
	return ans;
}
const int inv2 = qpow(2,M-2);
void get() {
	int n,m,c,f,i,j,k;
	cin>>n>>m>>c>>f;
	for(i=1; i<=n; i++) cin>>(s[i]+1);
	for(i=1; i<=n; i++) {
		r[i][m+1]=r[i][m+2]=0;
		for(j=m; j>=1; j--) {
			if(s[i][j]=='0') {
				r[i][j]=1+r[i][j+1];
			}
		}
	}
	int ansc=0;
	for(j=1; j<=m; j++) {
		int st=-1;
		for(i=1; i<=n+1; i++) {
			if(i==n+1||s[i][j]=='1') {
				if(st==-1) continue;
				int a1=0,a2=0,a3=0;
				for(k=st; k<i; k++) {
					a1+=r[k][j+1];
					a2+=r[k][j+1]*r[k][j+1]%M;
					a1%=M;
					a2%=M;
				}
				for(k=st+1; k<i; k++) {
					a3+=r[k-1][j+1]*r[k][j+1]%M;
					a3%=M;
				}
				ansc+=(a1*a1%M-a2)*inv2%M;
				ansc%=M;
				ansc-=a3;
				ansc%=M;
				st=-1;
			} else {
				if(st==-1) st=i;
			}
		}
	}
	int ansf=0;
	for(j=1; j<=m; j++) {
		int st=-1;
		for(i=1; i<=n+1; i++) {
			if(i==n+1||s[i][j]=='1') {
				if(st==-1) continue;
				for(k=st; k<i; k++) pre[k]=0;
				pre[i]=pre[i+1]=pre[i+2]=0;
				for(k=i-1; k>=st; k--) {
					pre[k]=pre[k+1]+(i-k-1)*r[k][j+1]%M;
					pre[k]%=M;
				}
				for(k=st; k<i; k++) {
					ansf+=r[k][j+1]*pre[k+2]%M;
					ansf%=M;
				}
				st=-1;
			} else {
				if(st==-1) st=i;
			}
		}
	}
	ansc*=c,ansf*=f;
	ansc=(ansc%M+M)%M,ansf=(ansf%M+M)%M;
	cout<<ansc<<" "<<ansf<<endl;
}
signed main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	//remember to close sync! here!
	ios::sync_with_stdio(0),cin.tie(0);
	int T,id;
	cin>>T>>id;
	while(T--) get();
	return 0;
}
