#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
inline int read(){
	int s=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
	return s*w;
}
int o[1003][1003];//the number of every line's 0
long long ansc=0,ansf=0;int t,id,n,m,c,f,ans1,ans2;
string s;
void c1(int i,int j,int ans1,int ans2){
	if(!o[i][j+1]||!o[i+1][j])return ;
	int i1=i,j1=j+1;
	while(o[i1][j1])j1++;
	j1--;
	ans1=o[i1][j1]-o[i][j];
	i1=i+2,j1=j;
	while(o[i1][j1]){
		
		j1=j+1;
		while(o[i1][j1])j1++;
		j1--;
		ans2=o[i1][j1]-o[i+2][j];
		ansc+=(ans1*ans2)%mod;
		i1++;
	}
	return ;
}
void f1(int i,int j,int ans1,int ans2){
	if(!o[i][j+1]||!o[i+1][j])return ;
	int i1=i,j1=j+1;
	while(o[i1][j1])j1++;
	j1--;
	ans1=o[i1][j1]-o[i][j];
	i1=i+2,j1=j;
	while(o[i1][j1]){
		j1=j+1;
		while(o[i1][j1])j1++;
		j1--;
		ans2=o[i1][j1]-o[i+2][j];
		int i2=i1+1;
		while(o[i2][j])ansf+=(ans1*ans2)%mod,++i2;
		ansf%=mod;
		i1++;
	}
	return ;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	t=read();id=read();
	while(t--){
		n=read();m=read();c=read();f=read();
		ansc=0,ansf=0;
		memset(o,0,sizeof(o));//empty
		for(int i=1;i<=n;++i){
			string s;cin>>s;
			if(s[0]-'0'==0)o[i][0]=1;
			for(int j=1;j<m;++j){
				if(s[j]-'0'==0)o[i][j]=o[i][j-1]+1;
			}
		}
			
		if(c){
			for(int i=1;i<=n-2;++i){
				for(int j=0;j<m-1;++j){
					if(o[i][j]) c1(i,j,0,0);
				}
			}
		}
		if(f&&m<=500){
			for(int i=1;i<n-2;++i){
				for(int j=0;j<m-1;++j){
					if(o[i][j]) f1(i,j,0,0);
				}
			}
		}
		printf("%lld %lld\n",ansc,ansf);
	}
	
	fclose(stdin);fclose(stdout);
	return 0;
}
