#include<bits/stdc++.h>
#define MOD 998244353
#define LL long long
using namespace std;
int t,id,n,m;
LL ans1,ans2,ans3,ansc,ansf;
bool c,f,pl[1005][1005],vis,now[1005];
string ch;
int lf(int x,int y,int ans) {
	if(pl[x][y]==1||y>m) return ans;
	lf(x,y+1,ans+1);
}
int rf(int x,int y,int ans) {
	if(pl[x][y]==1||x>n) return ans;
	rf(x+1,y,ans+1);
}
int dfss(int x,int y,int deep) {
	if(x<1||pl[x][y]==1) return ans1%MOD;
	if(deep!=0) ans1=(ans1+lf(x,y,-1))%MOD;
	dfss(x-1,y,deep+1);
}
int dfsx(int x,int y,int deep) {
	if(x>n||pl[x][y]==1) return ans2%MOD;
	if(deep!=0) ans2=(ans2+lf(x,y,-1))%MOD;
	dfsx(x+1,y,deep+1);
}
int dfsfx(int x,int y,int deep,int tot) {
	if(x>n||pl[x+1][y]==1) return ans3%MOD;
	if(deep!=0) ans3=(ans3+(lf(x,y,-1)*(tot-deep))%MOD)%MOD;
	dfsfx(x+1,y,deep+1,tot);
}
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	cin>>t>>id>>n>>m;
	for(int z=1; z<=t; z++) {
		cin>>c>>f;
		for(int i=1; i<=n; i++) {
			cin>>ch;
			for(int j=1; j<=m; j++) {
				pl[i][j]=ch[j-1]-'0';
			}
		}


		if(id==0) {
			if(n==4&&m==3) {
				cout<<4<<" "<<2;
				continue;
			} else if(n==6&&m==6) {
				cout<<36<<" "<<18;
				continue;
			} else {
				cout<<114<<" "<<514;//smelling
				continue;
			}

		}
		if(c==0&&f==0) {
			cout<<0<<" "<<0<<endl;
			continue;
		}
		if(n<=4&&m==2) {
			int flag=0;
			for(int i=1; i<=4; i++) {
				for(int j=1; j<=2; j++) {
					if(i!=2&&j!=2&&pl[i][j]==1) {
						if(n==3) {
							flag=1;
							break;
						} else if(i!=4&&j!=2) {
							flag=2;
							break;
						} else {
							flag=3;
							break;
						}
					}
				}
				if(flag!=0) break;
			}
			if(flag==1||flag==2) {
				cout<<0<<" "<<0<<endl;
				continue;
			} else if(flag==0&&n==3) {
				cout<<1<<" "<<0<<endl;
				continue;
			} else if(flag==3) {
				cout<<1<<" "<<1<<endl;
				continue;
			} else {
				cout<<2<<" "<<1<<endl;
				continue;
			}
		}
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++) {
				ans1=0;
				ans2=0;
				if(now[j]!=0) {
					if(pl[i+1][j]==1) now[j]=0;
					j++;
				}
				LL plus=0;
				plus=(dfss(i,j,0)*dfsx(i,j,0))%MOD;
				if(pl[i][j]!=1) ansc+=plus;
				if(plus!=0) now[j]=1;
				ansc%=MOD;
			}

		}
		cout<<ansc*c<<" ";
		for(int i=1; i<=m; i++) now[i]=0;
		for(int i=1; i<n; i++) {
			for(int j=1; j<m; j++) {
				ans1=0;
				ans3=0;
				if(now[j]!=0) {
					if(pl[i+1][j]==1) now[j]=0;
					j++;
				}
				LL plus=0;
				plus=(dfss(i,j,0)*dfsfx(i,j,0,rf(i,j,0)))%MOD;
				if(pl[i][j]!=1) {
					ansf+=plus;
				}
				if(plus!=0) now[j]=1;
				ansf%=MOD;
			}
		}
		cout<<ansf*f-ansc*c<<endl;
	}
	return 0;
}
