#include<bits/stdc++.h>
#define ll long long
#define IN inline
#define rg register
#define re(i,a,b) for(rg int i=a,i##up=b;i<=i##up;i++)
#define er(i,a,b) for(rg int i=a,i##dw=b;i>=i##dw;i--)
#define go(i,x) for(rg int i=head[x];i;i=nxt[i])
#define mem(a,x) memset(a,x,sizeof a)
const int N=1e3+5,ljc=998244353;
IN ll read(){
	rg ll res=0,f=1; rg char c; do f&=((c=getchar())!='-'); while(c<'0' || '9'<c);
	for(;'0'<=c && c<='9';res=res*10+(c^48),c=getchar()); return f?res:-res;
}
int n,m,c,f,a[N][N]; char s_[N];
ll rm[N][N],dm[N][N],s1[N][N],s2[N][N];
IN ll solC(){
	ll ans=0;
	re(i,1,n-2) re(j,1,m-1) if(rm[i][j]>j) (ans+=(rm[i][j]-j)*(s1[dm[i][j]][j]-s1[i+1][j])%ljc)%ljc;
	return (ans+ljc)%ljc;
}
IN ll solF(){
	ll ans=0;
	re(i,1,n-3) re(j,1,m-1) if(rm[i][j]>j) (ans+=(rm[i][j]-j)*(s2[dm[i][j]][j]-s2[i+1][j])%ljc)%ljc;
	return (ans+ljc)%ljc;
}
int main(){
	freopen("plant.in","r",stdin),freopen("plant.out","w",stdout);
	for(int T=read(),id_=read();T--;){
		n=read(),m=read(),c=read(),f=read();
		re(i,1,n){scanf("%s",s_+1); re(j,1,m) a[i][j]=s_[j]-'0';}
		re(i,1,n) re(j,1,m) if(!a[i][j]){
			int pos=j+1; for(;pos<=m && !a[i][pos];pos++);
			re(k,j,pos-1) rm[i][k]=pos-1; j=pos;
		}
		re(i,1,n) re(j,1,m) s1[i][j]=(s1[i-1][j]+(rm[i][j]-j)*(!a[i][j]))%ljc;
//		re(i,1,n) re(j,1,m) printf("%d%c",rm[i][j],j==m?'\n':' '); puts("");
//		re(i,1,n) re(j,1,m) printf("%d%c",s1[i][j],j==m?'\n':' '); puts("");
		re(j,1,m) re(i,1,n) if(!a[i][j]){
			int pos=i+1; for(;pos<=n && !a[pos][j];pos++);
			re(k,i,pos-1) dm[k][j]=pos-1; i=pos;
		}
		re(i,1,n) re(j,1,m) s2[i][j]=(s2[i-1][j]+(rm[i][j]-j)*(dm[i][j]-i)*(!a[i][j]))%ljc;
//		re(i,1,n) re(j,1,m) printf("%d%c",dm[i][j],j==m?'\n':' '); puts("");
//		re(i,1,n) re(j,1,m) printf("%d%c",s2[i][j],j==m?'\n':' '); puts("");
		printf("%lld %lld\n",c?solC():0,f?solF():0);
	}
	return 0;
}
