#include<bits/stdc++.h>
#define int long long
#define N 1005
#define MOD 998244353
using namespace std;
int n,m,C,F,lft[N][N],dn[N][N],c[N][N],f[N][N],sum[N];
char s[N][N];
void solve(){
	memset(c,0,sizeof(c));
	memset(f,0,sizeof(f));
	memset(lft,0,sizeof(lft));
	memset(dn,0,sizeof(dn));
	scanf("%lld %lld %lld %lld",&n,&m,&C,&F);
	for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
	for(int i=1;i<=n;i++){
		int tmp=0;
		for(int j=m;j>=1;j--){
			if(s[i][j]=='0'){
				lft[i][j]=tmp;
				tmp++;
			}
			else{
				lft[i][j]=0;
				tmp=0;
			}
		}
	}
	for(int j=1;j<=m;j++){
		int tmp=0;
		for(int i=n;i>=1;i--){
			if(s[i][j]=='0'){
				dn[i][j]=tmp;
				tmp++;
			}
			else{
				dn[i][j]=0;
				tmp=0;
			}
		}
	}
	for(int j=1;j<=m;j++){
		int lst=0;
		for(int i=1;i<=n;i++){
			if(s[i][j]=='1')lst=i,c[i][j]=0;
				else if(lst<i-2)c[i][j]=(sum[i-2]-sum[lst]+MOD)%MOD*lft[i][j]%MOD;
			sum[i]=sum[i-1]+lft[i][j];
		}
	}
	int sumc=0,sumf=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			sumc=(sumc+c[i][j])%MOD;
			sumf=(sumf+c[i][j]*dn[i][j]%MOD)%MOD;
		}
	printf("%lld %lld\n",sumc*C%MOD,sumf*F%MOD);
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,Type;scanf("%lld %lld",&T,&Type);
	while(T--)solve();
	return 0;
}