#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
	int w=1,s=0;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return w*s;
}
const int N=1010,mod=998244353;
int t,id;
int n,m,c,f;
int a[N][N];
ll dfs1(int x,int y){	//C
	ll sum=0;
	for(int i=x+2;i<=n;i++){
		if(a[i][y]) break;
		if(a[i][y+1]) continue;
		int j=y+1,num1=0,num2=0;
		while(!a[x][j]&&j<=m) j++,num1++;
		j=y+1;
		while(!a[i][j]&&j<=m) j++,num2++;
		sum+=num1%mod*num2%mod;
		sum%=mod;
	}
	return sum;
}
ll dfs2(int x,int y){	//F
	ll sum=0;
	for(int i=x+2;i<n;i++){
		if(a[i][y]) break;
		if(a[i][y+1]||a[i+1][y]) continue;
//		printf("%d\n",i);
		int j=y+1,num1=0,num2=0,num3=0;
		while(!a[x][j]&&j<=m) j++,num1++;
//		printf("y1=%d\n",num1);
		j=y+1;
		while(!a[i][j]&&j<=m) j++,num2++;
//		printf("y2=%d\n",num2);
		j=i+1;
		while(!a[j][y]&&j<=n) j++,num3++;
//		printf("x3=%d\n",num3);
		sum+=num1%mod*num2%mod*num3%mod;
		sum%=mod;
	}
//	printf("%d %d %d\n",x,y,sum);
	return sum;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	t=read(),id=read();
	while(t--){
		n=read(),m=read(),c=read(),f=read();
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				scanf("%1d",&a[i][j]);
		if(c==0&&f==0){
			printf("0 0\n");continue;
		} 
		ll ans1=0,ans2=0;
		//C
		if(c==0) printf("0 ");
		else{
			for(int i=1;i<=n-2;i++)
				for(int j=1;j<m;j++){
					if(a[i][j]||a[i][j+1]||a[i+1][j]||a[i+2][j]) continue;
					ans1+=dfs1(i,j);
					ans1%=mod;
	//				printf("%d %d %d\n",i,j,ans1);
				}
			printf("%d ",ans1*c%mod);
		}
		//F
		if(f==0) printf("0\n");
		else{
			for(int i=1;i<=n-3;i++)
				for(int j=1;j<m;j++){
					if(a[i][j]||a[i][j+1]||a[i+1][j]||a[i+2][j]||a[i+3][j]) continue;
					ans2+=dfs2(i,j);
					ans2%=mod;
	//				printf("%d %d %d\n",i,j,ans2);
				}
			printf("%d\n",ans2*f%mod);
		}
		
	}
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/
