#include<bits/stdc++.h>
using namespace std;
#define LL long long
//bool bg;
const LL P=998244353;
int n,m,c,f;
LL ans_c,ans_f;
LL a[1086][1086],r[1086][1086],d[1086][1086],sum_d1[1086][1086],sum_d2[1086][1086];
//bool ed;
void innt(){
	ans_c=ans_f=0;
}
LL mod(LL x){
	return (x>=P)?(x%P):x;
}
void work(){
	innt();
	scanf("%d%d%d%d",&n,&m,&c,&f);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			char ch;
			cin>>ch;
			a[i][j]=ch-'0';
		}
	}
	for(int i=1;i<=n;i++){
		LL now=0;
		for(int j=m;j>=1;j--){
			if(!a[i][j]){
				r[i][j]=now;
				now++;
			}
			else{
				r[i][j]=0;
				now=0;
			}
		}
	}
	for(int j=1;j<=m;j++){
		LL now1=0,now2=0,now3=0;
		for(int i=n;i>=1;i--){
			if(!a[i][j]){
				d[i][j]=now1;
				now1++;
				now2+=r[i][j];
				now2=mod(now2);
				now3+=r[i][j]*d[i][j];
				now3=mod(now3);
				sum_d1[i][j]=now2;
				sum_d2[i][j]=now3;
			}
			else{
				d[i][j]=sum_d1[i][j]=sum_d2[i][j]=0;
				now1=now2=now3=0;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]){
				continue;
			}
			if(c){
				if(d[i][j]>=2){
					ans_c=mod(ans_c+mod(r[i][j]*sum_d1[i+2][j]));
				}
			}
			if(f){
				if(d[i][j]>=2){
					ans_f=mod(ans_f+mod(r[i][j]*sum_d2[i+2][j]));
				}
			}
		}
	}
	printf("%lld %lld\n",ans_c,ans_f);
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
//	printf("%lld\n",&ed-&bg>>20);
	int T,id;
	scanf("%d%d",&T,&id);
	while(T--){
		work();
	}
	return 0;
}