#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 998244353
int read() {
	int s=0; char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c<='9'&&c>='0') s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return s;
}
int n,m,c,f,a[1005][1005],len[1005][1005],num;
int lem[1005][1005],Len[1005][1005];
string s;
ll work_C() {
	ll sum=0;
	for(int i=1;i<=m;i++) {
		for(int j=1;j<=n-2;j++) {
			if(a[j][i]==1) continue;
			for(int k=j+1;k<=n;k++) {
				if(a[k][i]==1) break;
				if(k>=j+2) {
					sum=(sum+1ll*lem[j][i]*lem[k][i]%mod)%mod;
				}
			}
		}
	}
	return sum;
}
ll work_F() {
	ll sum=0;
	for(int i=1;i<=m;i++) {
		for(int j=1;j<=n-3;j++) {
			if(a[j][i]==1) continue;
			for(int k=j+1;k<=n-1;k++) {
				if(a[k][i]==1||a[k+1][i]==1) break;
				if(k>=j+2) {
					sum=(sum+1ll*lem[j][i]*lem[k][i]%mod*Len[k+1][i]%mod)%mod;
				}
			}
		}
	}
	return sum;
}
void solve() {
	memset(Len,0,sizeof(Len));
	memset(lem,0,sizeof(lem));
	n=read(); m=read(); c=read(); f=read();
	for(int i=1;i<=n;i++) {
		cin>>s;
		for(int j=0;j<s.length();j++) {
			a[i][j+1]=s[j]-'0';
		}
	}
	num=0;
	for(int i=1;i<=n;i++) {
		num=0;
		for(int j=m;j>=1;j--) {
			if(a[i][j]==1) num=0;
			else lem[i][j]=num++;
		}
	}
	num=0;
	for(int i=1;i<=m;i++) {
		num=0;
		for(int j=n;j>=1;j--) {
			if(a[j][i]==1) num=0;
			else Len[j][i]=++num;
		}
	}
	if(c==1) printf("%lld ",work_C());
	else printf("0 ");
	if(f==1) printf("%lld\n",work_F());
	else printf("0\n");
}
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	T=read(); id=read();
	while(T--) solve();
	return 0;
}
