///I LOVE CCF !!!   PLEASE LET ME PASS !!!!    
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int MOD=998244353,MAXN=1005;
int a[MAXN][MAXN],n,m,T,id,sumx[MAXN][MAXN],sumy[MAXN][MAXN],c,f;
///I LOVE CCF !!!   PLEASE LET ME PASS !!!!    
int sloveC(){
	int ans=0;
	for(int j=1;j<=m;j++){
		for(int i=1;i<=n;i++){
			if(!sumy[i][j])continue;
			int start=i;
			while(sumy[start][j])start++;
			start--;
			if(start-i>=2){
				for(int yy=i+2;yy<=start;yy++){
					int startup=j,startdown=j,num=0;
					while(sumy[i][startup])startup++;
					while(sumy[yy][startdown])startdown++;
					startup--;startdown--;
					if((sumx[i][startup]-sumx[i][j])<=0||(sumx[yy][startdown]-sumx[yy][j])<=0)continue;
					num=max(num, (sumx[i][startup]-sumx[i][j]) * (sumx[yy][startdown]-sumx[yy][j]) ) % MOD;
					ans=(ans+num%MOD)%MOD;
				}
			}
		}
	}
	return ans%MOD;
}
///I LOVE CCF !!!   PLEASE LET ME PASS !!!!    
int sloveF(){
	int ans=0;
	for(int j=1;j<=m;j++){
		for(int i=1;i<=n;i++){
			if(!sumy[i][j])continue;
			int start=i;
			while(sumy[start][j])start++;
			start--;
			if(start-i>=2){
				for(int yy=i+2;yy<start;yy++){
					int startup=j,startdown=j,num=0;
					while(sumy[i][startup])startup++;
					while(sumy[yy][startdown])startdown++;
					startup--;startdown--;
					if((sumx[i][startup]-sumx[i][j])<=0||(sumx[yy][startdown]-sumx[yy][j])<=0||(sumy[start][j]-sumy[yy][j])<=0)continue;
					num=max(num, (sumx[i][startup]-sumx[i][j])%MOD * (sumx[yy][startdown]-sumx[yy][j])%MOD * (sumy[start][j]-sumy[yy][j])%MOD ) % MOD;
					ans=(ans+num%MOD)%MOD;
				}
			}
		}
	}
	return ans%MOD;
}
///I LOVE CCF !!!   PLEASE LET ME PASS !!!!    
void read(){
	string b;
	for(int i=1;i<=n;i++){
		int j=1;
		cin>>b;
		for(int k=0;k<b.length();k++)
			a[i][j++]=b[k]-'0';
	}
}
///I LOVE CCF !!!   PLEASE LET ME PASS !!!!    
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--){
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(int i=0;i<=1000;i++)
			for(int j=0;j<=1000;j++){
				sumx[i][j]=sumy[i][j]=0;
				a[i][j]=1;
			}
		read();
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				if(!a[i][j])sumx[i][j]=sumx[i][j-1]+1;
		for(int j=1;j<=m;j++)
			for(int i=1;i<=n;i++)
				if(!a[i][j])sumy[i][j]=sumy[i-1][j]+1;
		printf("%d %d\n",sloveC()*c,sloveF()*f);
	}
	return 0;
} 
///I LOVE CCF !!!   PLEASE LET ME PASS !!!!       