#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline LL read(){
	LL res=0,fl=1;
	char ch=getchar();
	while(!(ch>='0' && ch<='9')){
		if(ch=='-')fl=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')res=res*10+ch-'0',ch=getchar();
	return res*fl;
}
const LL mod=998244353;
const LL N=1e3+5;
inline LL max(LL a,LL b){
	return a>b?a:b;
}
LL n,m,c,f,a[N][N],b[N][N],cntc[N],cntf[N];
LL ansc,ansf;
char s[N];
inline void Init(){
	memset(b,0,sizeof b);
	memset(cntc,0,sizeof cntc);
	memset(cntf,0,sizeof cntf);
	ansc=ansf=0;
}
int main(){		
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	T=read(),id=read();
	while(T--){
		Init();
		n=read(),m=read(),c=read(),f=read();
		for(int i=1;i<=n;i++){
			scanf("%s",s+1);
			for(int j=1;j<=m;j++){
				a[i][j]=s[j]-'0';
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=m;j>=1;j--){
				if(a[i][j]==1)b[i][j]=0;
				else b[i][j]=b[i][j+1]+1;
			}
		}
		for(int j=1;j<=m;j++){
			int d=0;
			for(int i=1;i<=n;i++)cntc[i]=cntf[i]=0;
			for(int i=n;i>=1;i--){
				if(a[i][j]==1){
					d=0;
					cntc[i+1]=cntf[i+1]=0;
					continue;
				}
				else d++;
				cntc[i]=cntc[i+1]+max(0,b[i][j]-1);
				cntf[i]=cntf[i+1]+max(0,(b[i][j]-1)*(d-1)%mod);
				cntc[i]%=mod;
				cntf[i]%=mod;
			}
			for(int i=1;i<=n;i++){
				ansc+=max(0,max(0,b[i][j]-1)*cntc[i+2]%mod);
				ansf+=max(0,max(0,b[i][j]-1)*cntf[i+2]%mod);
				ansc%=mod;
				ansf%=mod;
			}
		}
		ansc=ansc*c%mod;
		ansf=ansf*f%mod;
		cout<<ansc<<' '<<ansf<<'\n';
	}
	return 0;
}

