#include <bits/stdc++.h>

using namespace std;

int read() {
	int x=0, f=0; char c=getchar();
	while (!isdigit(c)) f|=c=='-', c=getchar();
	while (isdigit(c)) x=(x<<3)+(x<<1)+(c^48), c=getchar();
	return f ? -x : x;
}

const int N=1e3+10, P=998244353;
int n, m, C, F, f[N][N], g[N][N];
char s[N][N];

int calcc() {
	int res=0;
	for (int j=1; j<=m; j++) {
		int sum=0, lst=n+1;
		for (int i=n; i>=1; i--) {
			if (s[i][j]=='1') {
				lst=i;
				sum=0;
				continue;
			}
			res=(res+1ll*f[i][j]*sum%P)%P;
			if (i+1<lst) sum=(sum+f[i+1][j])%P;
		}
	}
	return res;
}

int calcf() {
	for (int j=1; j<=m; j++) {
		g[n+1][j]=-1;
		for (int i=n; i>=1; i--) {
			if (s[i][j]=='1') g[i][j]=-1;
			else g[i][j]=g[i+1][j]+1;
		}
	}
	int res=0;
	for (int j=1; j<=m; j++) {
		int sum=0, lst=n+1;
		for (int i=n; i>=1; i--) {
			if (s[i][j]=='1') {
				lst=i;
				sum=0;
				continue;
			}
			res=(res+1ll*f[i][j]*sum%P)%P;
			if (i+2<lst) sum=(sum+f[i+1][j]*g[i+1][j])%P;
		}
	}
	return res;
}

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T=read(), id=read();
	while (T--) {
		n=read(), m=read(), C=read(), F=read();
		for (int i=1; i<=n; i++) scanf("%s", s[i]+1);
		for (int i=1; i<=n; i++) {
			f[i][m+1]=-1;
			for (int j=m; j>=1; j--) {
				if (s[i][j]=='1') f[i][j]=-1;
				else f[i][j]=f[i][j+1]+1;
			}
		}
		if (C) printf("%d ", calcc());
		else printf("0 ");
		if (F) printf("%d\n", calcf());
		else printf("0\n");
	}
	return 0;
}