#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5,mod=998244353;
int n,m,fgc,fgf;
int ansc,ansf;
char s[N][N];
int d[N][N];
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,koishi;
	scanf("%d%d",&T,&koishi);
	while(T--) {
		scanf("%d%d%d%d",&n,&m,&fgc,&fgf);
		for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
		ansc=ansf=0;
		
		for(int i=1;i<=n;i++) {
			d[i][m+1]=0;
			for(int j=m;j>=1;j--) {
				d[i][j]=d[i][j+1]+1;
				if(s[i][j]=='1') d[i][j]=0;
			}
		}
		
		for(int i=1;i<=m;i++) {
			int sum=0;
			for(int j=1;j<=n;j++) {
				if(s[j][i]=='1') {
					sum=0;
					continue;
				}
				int t=d[j][i]-1;
				ansc+=t*1ll*sum%mod;
				if(ansc>=mod) ansc-=mod;
				
				if(j>=2&&s[j-1][i]=='0') sum+=d[j-1][i]-1;
			}
		}
		
		
		for(int i=1;i<=m;i++) {
			int sum=0,num=0;
			for(int j=1;j<=n;j++) {
				if(s[j][i]=='1') {
					sum=num=0;
					continue;
				}
				int t=d[j][i]-1;
				ansf+=num;
				if(ansf>=mod) ansf-=mod;
				
				if(j>=3&&s[j-1][i]=='0'&&s[j-2][i]=='0') {
					sum+=d[j-2][i]-1;
				}
				
				num+=sum*1ll*t%mod;
				if(num>=mod) num-=mod;
			}
		}
		
		
		printf("%d %d\n",ansc*fgc,ansf*fgf);
	}
	return 0;
}
