#include<bits/stdc++.h>
#define maxn 1005
#define int long long
#define TT 998244353
using namespace std;
int T,id,n,m,c,f,vc,vf,ans;
int dn[maxn][maxn],rt[maxn][maxn],st[maxn],nxt[maxn][2];
char s[maxn][maxn];
inline int read(){
	int ret=0;char f=1,ch=getchar();
	while(!isdigit(ch)) f=(ch=='-'?-f:f),ch=getchar();
	while(isdigit(ch)) ret=ret*10+ch-'0',ch=getchar();
	return ret*f;
}
signed 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();
		memset(dn,0,sizeof dn);vc=vf=0;memset(rt,0,sizeof rt);
		memset(s,0,sizeof s);memset(nxt,0,sizeof nxt);memset(st,0,sizeof st);
		for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
		for(int i=n;i>=1;i--){
			for(int j=m;j>=1;j--){
				if(s[i][j]=='1') continue;rt[i][j]=dn[i][j]=1;
				dn[i][j]+=dn[i+1][j],rt[i][j]+=rt[i][j+1];
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<m;j++){
				if(s[i][j]=='1'){st[j]=nxt[j][0]=nxt[j][1]=0;continue;}
				if(s[i][j]=='0'&&s[i-1][j]=='0'&&s[i][j+1]=='0'){
					vc=vc+(rt[i][j]-1)*(st[j])%TT;
					if(s[i+1][j]=='0') vf=vf+(rt[i][j]-1)*(dn[i][j]-1)*(st[j])%TT;
				}
				if(s[i][j]=='0'&&s[i+1][j]=='0'&&s[i][j+1]=='0') nxt[j][1]+=rt[i][j]-1;
			}
			for(int j=1;j<m;j++) st[j]+=nxt[j][0],nxt[j][0]=nxt[j][1],nxt[j][1]=0;
		}
		printf("%lld %lld\n",vc*c%TT,vf*f%TT);
	}
	return 0;
}