#include<bits/stdc++.h>
#define maxn 1005
#define int long long
using namespace std;
const int TT=998244353;
int n,T,A[maxn][maxn],m,f[maxn][maxn],C,F,g[maxn][maxn],d[maxn][maxn],Ansc,t[maxn][maxn],Ansf;
char s[maxn][maxn];
inline int read(){
	int ret=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-f;ch=getchar();}
	while (ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return ret*f;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),read();
	for (int Tt=1;Tt<=T;Tt++){
		n=read(),m=read(),C=read(),F=read(),Ansc=0,Ansf=0;
		memset(f,0,sizeof f),memset(g,0,sizeof g),memset(d,0,sizeof d);
		for (int i=1;i<=n;i++){
			scanf("%s",s[i]);
			for (int j=1;j<=m;j++) A[i][j]=(s[i][j-1]=='0');
		}
		for (int i=n;i;i--)
		for (int j=m;j;j--) if (A[i][j]){
			if (A[i][j-1]) g[i][j-1]=g[i][j]+1;
			if (A[i-1][j]) f[i-1][j]=f[i][j]+g[i][j],t[i-1][j]=(t[i][j]+g[i][j]*d[i][j])%TT,d[i-1][j]=d[i][j]+1;
			Ansc=(Ansc+g[i][j]*f[i+1][j])%TT,Ansf=(Ansf+g[i][j]*t[i+1][j])%TT;
		}
//		for (int i=1;i<=n;i++,putchar('\n'))
//		for (int j=1;j<=m;j++) printf("%lld ",t[i][j]);
		printf("%lld %lld\n",Ansc*C%TT,Ansf*F%TT);
	}
	return 0;
}