#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
int mp[1001][1001];
char s[1001][1001];
struct node{
	int l,r;
}x1[1001],x2[1001];
int n,m,c,f;
int vc;
int vf;
int T,id;
int zc(int x,int y){
	if(mp[x][y]==0&&mp[x+1][y]==0&&mp[x+2][y]==0&&mp[x][y+1]==0)return 1;
	return 0;
}
int chu(int x,int y){
	int sum=0;
	for(int i=x;i<=n;i++){
		if(mp[i][y]==0)sum++;
		else break;
	}
	return sum;
}
int zha(int x,int y){
	int sum=0;
	for(int i=y;i<=m;i++){
		if(mp[x][i]==0)sum++;
		else break;
	}
	return sum;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--){
		memset(mp,0x3f,sizeof(mp));
		scanf("%d%d%d%d",&n,&m,&c,&f);
		vc=vf=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)
			cin>>s[i][j];
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				mp[i][j]=s[i][j]-'0';
			}
		}
		int t=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(zc(i,j)==1){
					x1[++t].l=i;
					x1[t].r=j;
				}
			}
		}
		for(int o=1;o<=t;o++){
			int lc=0;
			for(int j=x1[o].r;j<=m;j++){
				if(mp[x1[o].l][j]==0)lc++;
				else break;
			}
			int kc=0;
			for(int i=x1[o].l;i<=n;i++){
				if(mp[i][x1[o].r]==0)kc++;
				else break;
			}
			for(int i=x1[o].l+kc-1;i>=x1[o].l+2;i--){
				if(zha(i,x1[o].r)==1)continue;
				vc+=(((lc-1)%mod)*((zha(i,x1[o].r)-1)%mod))%mod;
				if(chu(i,x1[o].r)==0)continue;
				else vf+=((((lc-1)*(zha(i,x1[o].r)-1))%mod)*((chu(i,x1[o].r)-1)%mod))%mod;
			}
		}
		printf("%d %d\n",(vc*c)%mod,(vf*f)%mod);
	}
	return 0;
}