#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int M=1007,Mod=998244353;
ll T,id,n,m,c,f,ansc,ansf,sh,xia,x;
ll rght[M][M],dwn[M][M],suf,sufc;
char s[M][M];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%lld%lld",&T,&id);
	while(T--){
		ansc=ansf=0;
		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++) rght[i][m+1]=-1;
		for(int i=1;i<=m;i++) dwn[n+1][i]=-1;
		for(int i=m;i>=1;i--)
			for(int j=1;j<=n;j++){
				if(s[j][i]=='1') rght[j][i]=-1;
				else rght[j][i]=rght[j][i+1]+1;
			}
		for(int i=n;i>=1;i--)
			for(int j=1;j<=m;j++){
				if(s[i][j]=='1') dwn[i][j]=-1;
				else dwn[i][j]=dwn[i+1][j]+1;
			}
		for(int j=1;j<=m;j++){
			suf=0,sufc=0;
			for(int i=n;i>=1;i--){
				if(s[i][j]=='1'){
					suf=sufc=0;
					continue;
				}
				ansf=(ansf+suf*rght[i][j])%Mod;
				ansc=(ansc+sufc*rght[i][j])%Mod;
				suf=(suf+dwn[i][j]*rght[i][j])%Mod;
				sufc=(sufc+rght[i][j])%Mod;
			}
			for(int i=1;i<n;i++)
				if(s[i][j]=='0' && s[i+1][j]=='0')
					ansc=(ansc-rght[i][j]*rght[i+1][j]%Mod+Mod)%Mod,
					ansf=(ansf-rght[i][j]*rght[i+1][j]%Mod*dwn[i+1][j]%Mod+Mod)%Mod;
		}
		printf("%lld %lld\n",ansc*c,ansf*f);
	}
	return 0;
}