#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define lson (u<<1)
#define rson (u<<1|1)
LL read() {
	LL x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') {
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
char readC() {
	char c=getchar();
	while(c<'0'||c>'9') {
		c=getchar();
	}
	return c;
}

const LL MOD=998244353;
LL add(LL x,LL y) {
	return (x%MOD+y%MOD+MOD)%MOD;
}
LL mul(LL x,LL y) {
	return x%MOD*y%MOD;
}
int n,m,t1,t2;
#define MAXN 1002
int a[MAXN][MAXN];
int f[MAXN][MAXN],g[MAXN][MAXN];
inline void work() {
	n=read(),m=read(),t1=read(),t2=read();
	for(int i=1; i<=n; i++)for(int j=1; j<=m; j++) {
			a[i][j]=readC()-'0';
		}
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	for(int i=1; i<=n; i++) {
		for(int j=m; j>=1; j--) {
			if(!a[i][j])f[i][j]=f[i][j+1]+1;
			else f[i][j]=0;
		}
	}
	for(int j=m; j>=1; j--)for(int i=n; i>=1; i--) {
			if(!a[i][j])g[i][j]=g[i+1][j]+1;
			else g[i][j]=0;
		}
	LL res1=0,res2=0;

	//res1
	for(int j=1; j<=m; j++) {
		LL tmp=0;
		for(int i=1; i<=n; i++) {
			if(a[i][j])tmp=0;
			else {
				res1=add(res1,mul(f[i][j]-1,tmp));
				if(!a[i-1][j]&&i!=1)
					tmp=add(tmp,f[i-1][j]-1); //TODO 3
			}
		}
	}
	//res2
	for(int j=1; j<=m; j++) {
		LL tmp=0;
		for(int i=1; i<=n; i++) {
			if(a[i][j])tmp=0;
			else {
				res2=add(res2,mul(mul(f[i][j]-1,tmp),g[i][j]-1));
				if(!a[i-1][j]&&i!=1)
					tmp=add(tmp,f[i-1][j]-1); //TODO 3
			}
		}
	}

	printf("%lld %lld\n",res1*t1,res2*t2);
}
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(),id=read();
	while(T--)work();
}
