#include<bits/stdc++.h>
const int mx=1e3+5;
const int mod=998244353;
using namespace std;
int n,m,tid,T,c,f;
bool mymap[mx][mx];
int visl[mx],vish[mx];
long long pres,unders;
long long ansc=0,ansf=0;
char a[mx];
int checkh(int x,int y) {
	if(vish[y]==n+1)return n+1;
	if(x<vish[y])return vish[y];
	for(int i=x+1; i<=n; i++) {
		if(mymap[i][y]) {
			vish[y]=i;
			return i;
		}
	}
	return n+1;
}
int checkl(int x,int y) {
	if(visl[x]==m+1)return m+1;
	if(y<visl[x])return visl[x];
	for(int i=y+1; i<=m; i++) {
		if(mymap[x][i]) {
			visl[x]=i;
			return i;
		}
	}
	return m+1;
}
void dealf() {
	for(int i=1; i<=n-2; i++) {
		for(int j=1; j<=m-1; j++) {
			if(mymap[i][j])continue;
			pres=checkl(i,j)-j-1;
			memset(visl,0,sizeof(visl));
			int hangshu=checkh(i,j);
			if(hangshu==0)continue;
			for(int k=i+1; k<=n-1; k++) {
				if(mymap[k][j]==1)break;
				int lt=checkl(k,j);
				if(k>=i+2) {
					unders=lt-j-1;
					ansf+=(((long long)pres*unders*(hangshu-k-1))*f)%mod;
					ansf=ansf%mod;
				}
			}
		}
	}
}
void dealc() {
	for(int i=1; i<=n-2; i++) {
		for(int j=1; j<=m-1; j++) {
			if(mymap[i][j]==1)continue;
			memset(visl,0,sizeof(visl));
			int tep=checkl(i,j);
			pres=tep-j-1;
			for(int k=i+1; k<=n; k++) {
				if(mymap[k][j]==1)break;
				if(k>=i+2) {
					int lt=checkl(k,j);
					unders=lt-j-1;
					ansc+=(((long long)pres*unders)*c)%mod;
					ansc=ansc%mod;
				}
			}
		}
	}
}
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&tid);
	while(T--) {
		scanf("%d%d%d%d",&n,&m,&c,&f);
		if(c==0&&f==0) {
			printf("0 0\n");
			continue;
		}
		for(int i=1; i<=n; i++) {
			scanf("%s",a);
			for(int j=1,k=0; j<=m; j++,k++) {
				if(a[k]=='0')mymap[i][j]=0;
				if(a[k]=='1')mymap[i][j]=1;
			}
		}
		if(tid==5) {
			int tap=m;
			m=2;
			dealc();
			memset(visl,0,sizeof(visl));
			dealf();
			memset(visl,0,sizeof(visl));
			memset(vish,0,sizeof(vish));
			ansc=ansc*((int)(m+2)/3);
			ansc%=mod;
			ansf=ansf*((int)(m+1)/3);
			ansf%=mod;
			printf("%lld %lld\n",ansc,ansf);
			ansc=0;
			ansf=0;
			continue;
		}
		if(tid==6) {
			printf("%lld 0\n",(long long)(m-1)*(m-1)*(n/4)%mod);
			continue;
		}
		dealc();
		memset(visl,0,sizeof(visl));
		printf("%lld ",ansc);
		dealf();
		printf("%lld\n",ansf);
		ansc=0;
		ansf=0;
		memset(visl,0,sizeof(visl));
		memset(vish,0,sizeof(vish));
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
