#include<bits/stdc++.h>
#define rr register
#define TT 998244353
#define maxn 1005
using namespace std;
inline int read(){
	rr int num=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9') num=(num<<3)+(num<<1)+ch-'0',ch=getchar();
	return f*num;
}
int T,id,n,m,l[maxn][maxn],d[maxn][maxn],c,f;
int suml[maxn][maxn],sumd[maxn][maxn];
int nol[maxn][maxn],nod[maxn][maxn];
int ansc,ansf;
bool v[maxn][maxn];
void add(int x,int y){
	rr int numl=0,numd=0;
	for(rr int j=y-1;j>=1;j--){
		if(v[x][j]) break;
		nol[x][j]=y;
		l[x][j]=numl++;
	}
	for(rr int i=x-1;i>=1;i--){
		if(v[i][y]) break;
		nod[i][y]=x;
		d[i][y]=numd++;
	}
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),id=read();
	while(T--){
		n=read(),m=read(),c=read(),f=read();
		ansc=ansf=0;
		memset(v,1,sizeof v);
		memset(suml,0,sizeof suml);
		memset(sumd,0,sizeof sumd);
		memset(nod,0,sizeof nod);
		memset(nol,0,sizeof nol);
		memset(l,0,sizeof l);
		memset(d,0,sizeof d);
		
		for(rr int i=1;i<=n;i++)
		for(rr int j=1;j<=m;j++){
			rr char ch=getchar();
			while(ch!='0'&&ch!='1') ch=getchar();
			v[i][j]=ch-'0';
		}
		
		for(rr int i=1;i<=n+1;i++)
		for(rr int j=1;j<=m+1;j++)
			if(v[i][j]) add(i,j);
		
		for(rr int i=1;i<=n;i++)
		for(rr int j=1;j<=m;j++)
			d[i][j]=d[i][j]*l[i][j]%TT;
		
		for(rr int i=n;i>=1;i--)
		for(rr int j=m;j>=1;j--){
			if(v[i][j]) continue;
			suml[i][j]=(suml[i+1][j]+l[i][j])%TT;
			sumd[i][j]=(sumd[i+1][j]+d[i][j])%TT;
		}
		
//		for(rr int i=1;i<=n;i++)
//		for(rr int j=1;j<=m;j++)
//			j==m?printf("%d\n",suml[i][j]):printf("%d ",suml[i][j]);
		
		for(rr int i=1;i<=n;i++)
		for(rr int j=1;j<=m;j++){
//			printf("%d ",suml[i+2][j]-suml[nod[i][j]][j]);
			if(v[i][j]||v[i+1][j]||v[i][j+1]) continue;
			ansc=(ansc+l[i][j]*(suml[i+2][j]-suml[nod[i][j]][j])%TT)%TT;
			if(v[i+2][j]||v[i+3][j]) continue;
			ansf=(ansf+l[i][j]*(sumd[i+2][j]-sumd[nod[i][j]][j])%TT)%TT;
		}
		
		printf("%d %d\n",ansc*c,ansf*f);
	}
	return 0;
}
