#include<bits/stdc++.h>
using namespace std;
long long M=998244353,f[1010][1010],g[1010][1010];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	long long T,idd;
	cin>>T>>idd;
	while(T--){
		memset(f,0,sizeof(f));
		long long a,b,c,d,ans1=0,ans2=0;
		cin>>a>>b>>c>>d;
		for(long long i=1;i<=a;i++){
			string str;
			cin>>str;
			str=" "+str;
			for(long long j=b;j>=1;j--){
				if(str[j]=='0')f[i][j]=f[i][j+1]+1;
				else f[i][j]=0;
				if(str[j]=='1')g[i][j]=1;
				else g[i][j]=0;
			}
		}
		if(c){
			for(long long i=1;i<=b;i++){
				long long ss=0;
				for(long long j=1;j<=a+1;j++){
					if((j==a+1||g[j][i]==1)&&g[j-1][i]==0){
						long long r=j-1,l=j-ss;
						if(ss<=2){
							ss=0;
							continue;
						}
						long long sum=f[r][i]-1;
						for(long long kk=r-2;kk>=l;kk--){
							ans1+=(f[kk][i]-1)*sum;
							ans1%=M;
							sum+=f[kk+1][i]-1;
							sum%=M;
						}
						ss=0;
					}
					if(g[j][i]==0)ss++;
				}
			}
		}
		if(d){
			for(long long i=1;i<=b;i++){
				long long ss=0;
				for(long long j=1;j<=a+1;j++){
					if((j==a+1||g[j][i]==1)&&g[j-1][i]==0){
						long long r=j-1,l=j-ss;
						if(ss<=3){
							ss=0;
							continue;
						}
						long long sum=f[r-1][i]-1;
						for(long long kk=r-3;kk>=l;kk--){
							ans2+=(f[kk][i]-1)*sum;
							ans2%=M;
							sum+=(f[kk+1][i]-1)*(r-kk-1);
							sum%=M;
						}
						ss=0;
					}
					if(g[j][i]==0)ss++;
				}
			}
		}
		cout<<ans1<<" "<<ans2<<"\n";
	}
	return 0;
}