#include<bits/stdc++.h>
#define mod 998244353
using namespace std;

inline int read(){
	int n=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)){n=n*10+ch-'0';ch=getchar();}
	return n*f;
}

const int N=1e3+10;
int n,m,c,f,T,id,ans1,ans2;
char a[N][N];
void solve(){
	n=read(),m=read(),c=read(),f=read();
	for(int i=1;i<=n;i++){
		cin>>a[i]+1;
	}
	if(!c&&!f){
		puts("0 0");
		return;
	}
	if(id==5){//A
		int p=m/3,yu=m%3;
		int b=p+(yu==2),ans1=0,ans2=0;
		for(int i=1;i<=n-2;i++) ans1+=n-i-1;
		ans2=ans1-(n-2);
		ans1*=b;
		ans2*=b;
		printf("%d %d\n",ans1*c%mod,ans2*f%mod);
		return;
	}
	if(id==6){//B
		puts("0 0");
		return;
	}
	for(int y0=1;y0<=m-1;y0++){
		for(int x1=1;x1<=n-2;x1++){
			for(int x2=x1+2;x2<=n;x2++){
				for(int y1=y0+1;y1<=m;y1++){
					for(int y2=y0+1;y2<=m;y2++){
						bool fg=0;
						for(int i=y0;i<=y1;i++) if(a[x1][i]=='1'){fg=1;break;}
						if(fg) continue;
						for(int i=y0;i<=y2;i++) if(a[x2][i]=='1'){fg=1;break;}
						if(fg) continue;
						for(int i=x1;i<=x2;i++) if(a[i][y0]=='1'){fg=1;break;}
						if(fg) continue;
						ans1++;
					}
				}
			}
		}
	}
	for(int y0=1;y0<=m-1;y0++){
		for(int x1=1;x1<=n-2;x1++){
			for(int x2=x1+2;x2<=n-1;x2++){
				for(int x3=x2+1;x3<=n;x3++){
					for(int y1=y0+1;y1<=m;y1++){
						for(int y2=y0+1;y2<=m;y2++){
							bool fg=0;
							for(int i=y0;i<=y1;i++) if(a[x1][i]=='1'){fg=1;break;}
							if(fg) continue;
							for(int i=y0;i<=y2;i++) if(a[x2][i]=='1'){fg=1;break;}
							if(fg) continue;
							for(int i=x1;i<=x3;i++) if(a[i][y0]=='1'){fg=1;break;}
							if(fg) continue;
							ans2++;						
						}
					}
				}
			}
		}
	}
	printf("%d %d\n",ans1*c%mod,ans2*f%mod);
}
int main(){	
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),id=read();
	while(T--) solve();
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/