#include <bits/stdc++.h>
#define rep(i, l, r) for(int i=l, _=r; i<=_; ++i)
typedef long long ll;
inline int read() {
	int res=0; char ch; bool k=1;
	while(!isdigit(ch=getchar())) k=ch^'-';
	do res=res*10+(ch^48); while(isdigit(ch=getchar()));
	return k? res: -res;
}
const int mN=1e3+9, mod=998244353;
int n, m, c, f;
char ch[mN][mN];
int tor[mN][mN], tod[mN][mN];
int sum[mN][mN];

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);

	int T_T=read(), id=read();
	while(T_T--) {
		memset(tor, 0, sizeof tor);
		memset(tod, 0, sizeof tod);
		memset(sum, 0, sizeof sum);
		int ansc=0, ansf=0;

		n=read(), m=read(), c=read(), f=read();
		rep(i, 1, n) {
			scanf("\n%s", ch[i]+1);
			for(int j=m; j>=1; --j) if(ch[i][j]=='1') {
				tor[i][j]=0;
				sum[i][j]=0;
			} else {
				tor[i][j]=tor[i][j+1]+1;
				sum[i][j]=sum[i-1][j]+tor[i][j]-1;
			}
		}
		for(int i=n; i>=1; --i) rep(j, 1, m) if(ch[i][j]=='1') {
			tod[i][j]=0;
		} else {
			tod[i][j]=tod[i+1][j]+1;
			if(i>2 && ch[i-1][j]=='0') {
				ll d=(ll) sum[i-2][j]*(tor[i][j]-1);
				ansc=(ansc+d)%mod;
				ansf=(ansf+d%mod*(tod[i][j]-1))%mod;
			}
		}
		printf("%d %d\n", ansc*c, ansf*f);
	}
	return 0;
}
/*
5+9+7

*/