#include<bits/stdc++.h>
using namespace std;
const int N=1005;
const int mod=998244353;
inline int read(){
	int t=1,x=0;
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') t=-1;
	for(;isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
	return t*x;
}
int t,id;
int n,m,c,f;
long long ans1,ans2;
int a[N][N];
void solve(int wide,int l,int r){
	int cnt1=0,cnt2=0;
	int i=wide,j=wide;
	while(1){
		if(!a[l][i] && i<=m) cnt1++;
		else break;
		i++;
	}
	while(1){
		if(!a[r][j] && j<=m) cnt2++;
		else break;
		j++;
	}
	long long len=0;
	if(f!=0){
		for(int i=r+1;i<=n;i++){
			if(!a[i][wide]) len++;
			else break;
		}
	}
	ans1=(ans1+((cnt1-1)%mod*(cnt2-1)%mod)%mod)%mod;
	if(f!=0) ans2=(ans2+(((cnt1-1)%mod*(cnt2-1)%mod)%mod*len%mod))%mod;
}

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	t=read(),id=read();
	while(t--){
		ans1=0,ans2=0;
		n=read(),m=read(),c=read(),f=read();
		if(c==0 && f==0){
			cout<<"0 0"<<endl;
			continue;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				char ch;
				cin>>ch;
				a[i][j]=(ch^48);
			}
		}
		if(n>500 && m>500 && f){
			int flag1=1,flag2=1;
			for(int i=1;i<=n;i++){
				for(int j=1;j<=m/3;j++){
					if(!a[i][j]) flag1=0;
				}
			}
			for(int i=1;i<=n/4;i++){
				for(int j=1;j<=m;j++){
					if(!a[i][j]) flag2=0;
				}
			}
			if(flag1) m=m/3+1;
			if(flag2) n=n/4+1;
		}
		for(int j=1;j<m;j++){
			for(int i=1;i<=n;i++){
				if(!a[i][j]){
					int len=0;
					for(int w=i;w<=n;w++){
						if(!a[w][j]) len++;
						else if(a[w][j]){
							if(len>=3 && !a[w-1][j+1] && !a[i][j+1]) solve(j,i,w-1);
							break;
						}
						if(w==n || len>=3){
							if(len>=3 && !a[w][j+1] && !a[i][j+1]) solve(j,i,w);
						}
					}
				}
			}
		}
		printf("%lld %lld\n",ans1%mod,ans2%mod);
	}
	fclose(stdin),fclose(stdout);
	return 0;
}
