#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1005;
const int MOD =998244353;
int n,m,c,f,T,opt;
int a[N][N];
char s[N][N];
ll sumc,sumf,tot[N][N],sum[N][N],down[N][N],sum2[N][N];
int read(){
	int num=0,k=1;char c='&';
	while(c<'0' or c>'9'){
		c=getchar();
		if(c=='-') k=-1;
	}
	while(c>='0' and c<='9'){
		num=(num<<3)+(num<<1)+c-'0';
		c=getchar();
	}
	return num*k;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read();opt=read();
	while(T--){
		n=read();m=read();c=read();f=read();
		for(int i=1;i<=n;i++){
			scanf("%s",s[i]+1);
		}
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				a[i][j]=s[i][j]-'0';
		for(int i=1;i<=n;i++){
			int cnt=0;
			for(int j=m;j>=1;j--){
				if(a[i][j]) cnt=tot[i][j]=0;
				else tot[i][j]=cnt,cnt++;
			}
		}
		for(int j=1;j<=m;j++){
			int cnt=n;
			for(int i=n;i>=1;i--){
				if(a[i][j]) cnt=i-1;
				else down[i][j]=cnt;
			}
		}
		for(int j=1;j<=m;j++)
			for(int i=1;i<=n;i++)
					sum[i][j]=(tot[i][j]+sum[i-1][j])%MOD;
		for(int j=1;j<=m;j++)
			for(int i=1;i<=n;i++)
					sum2[i][j]=(tot[i][j]*(down[i][j]-i)+sum2[i-1][j])%MOD;
		for(int i=1;i<=n-2;i++)
			for(int j=1;j<=m;j++)
				sumc=(sumc+1ll*tot[i][j]*max(0ll,(sum[down[i][j]][j]-sum[i+1][j])))%MOD;
		for(int i=1;i<=n-3;i++)
			for(int j=1;j<=m;j++)
				sumf=(sumf+1ll*tot[i][j]*max(0ll,(sum2[down[i][j]-1][j]-sum2[i+1][j])))%MOD;
		printf("%lld %lld\n",sumc*c,sumf*f);
		sumc=sumf=0;
	}
	return 0;
}
