//100opts?
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N=1010;
const int MOD=998244353;

int Map[N][N];
int n,m,c,f;
int Vc,Vf;
int rig[N][N];
int don[N][N],drsum[N];

void init(){
	memset(Map,0,sizeof(Map));
	memset(rig,0,sizeof(rig));
	memset(don,0,sizeof(don));
	n=m=c=f=0;
	Vc=Vf=0;
}

void into(){
	char k[N];
	scanf("%lld%lld%lld%lld",&n,&m,&c,&f);
	for(int i=1;i<=n;++i){
		cin>>k;
		for(int j=1;j<=m;++j)
			Map[i][j]=k[j-1]-'0';
	}
}

void solve_init(){
	for(int i=n;i>0;--i){
		int now=0;
		for(int j=m;j>0;--j){
			if(Map[i][j]==1)
				now=0;
			else{
				rig[i][j]=now;
				++now;
			}
			if(Map[i][j]==1){
				don[i][j]=0;
				continue;
			}
			don[i][j]=don[i+1][j]+1;
		}
	}
}

void solvec(){
	for(int j=1;j<=m;++j){
		int sum=0;
		for(int i=n;i>0;--i){
			if(Map[i][j]==1){
				sum=0;
				continue;
			}
			if(rig[i][j]==0)	continue;
			if(rig[i+1][j]==0)
				Vc=(Vc+sum*rig[i][j]%MOD)%MOD;
			else
				Vc=(Vc+(sum-rig[i+1][j])*rig[i][j]%MOD)%MOD;
			sum+=rig[i][j];
		}
	}
}

void solvef(){
	for(int j=1;j<=m;++j){
		int sum=0;
		for(int i=n;i>0;--i){
			if(Map[i][j]==1){
				sum=0;
				continue;
			}
			if(rig[i][j]==0||don[i][j]==1)	continue;
			if(rig[i+1][j]==0)
				Vf=(Vf+sum*rig[i][j]%MOD)%MOD;
			else
				Vf=((Vf+(sum-(don[i+1][j]-1)*rig[i+1][j])*rig[i][j])%MOD)%MOD;
			sum+=(don[i][j]-1)*rig[i][j];
		}
	}
}

void solve(){
	solve_init();
	solvec();
	solvef();
}

signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int t,id;
	scanf("%lld%lld",&t,&id);
	while(t--){
		init();
		into();
		solve();
//		debug();
		printf("%lld %lld\n",Vc*c,Vf*f);
	}
	return 0;
}
