#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
#define foR(i,j,k) for(int i=(j);i>=(k);i--)
#define mst(a,b) memset(a,b,sizeof(a)); 
using namespace std;
int read() { int x; scanf("%d",&x); return x; }
const int P=998244353; 
int add(int x,int y) { return (x+y)%P; }
int sub(int x,int y) { return (x-y+P)%P; }
int mul(int x,int y) { return 1ull*x*y%P; }
void addmod(int &x,int y) { x=add(x,y); }
void submod(int &x,int y) { x=sub(x,y); }
void mulmod(int &x,int y) { x=mul(x,y); }
void fprint(int *a,int n) {For(i,1,n) fprintf(stderr,"%d%c",a[i]," \n"[i==n]); }
const int MN=1505+5; 
char s[MN][MN]; int n,m,pc,pf,len[MN][MN],down[MN][MN],prc[MN][MN],prf[MN][MN]; 
void works() {
	n=read(),m=read(),pc=read(),pf=read(); 
	mst(len,0); mst(down,0); 
	For(i,1,n) scanf("%s",s[i]+1); 
	foR(j,m,1) {
		For(i,1,n) {
			if(s[i][j]=='1') len[i][j]=0; 
			else len[i][j]=len[i][j+1]+1; 
		}
	}
	foR(i,n,1) {
		For(j,1,m) {
			if(s[i][j]=='1') down[i][j]=0; 
			else down[i][j]=down[i+1][j]+1; 
		}
	}
	int ansc=0,ansf=0; 
	foR(i,n,1) For(j,1,m) {
		if(s[i][j]=='1') continue; 
		if(i+2<=i+down[i][j]-1) prc[i][j]=add(prc[i+1][j],len[i+2][j]-1); 
		addmod(ansc,mul(len[i][j]-1,prc[i][j])); 
		if(i+2<=i+down[i][j]-1) prf[i][j]=add(prf[i+1][j],mul(down[i+2][j]-1,len[i+2][j]-1)); 
		addmod(ansf,mul(len[i][j]-1,prf[i][j])); 
	}
	printf("%d %d\n",mul(ansc,pc),mul(ansf,pf));
}
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(),tp=read();
	while(T--) {
		works(); 
	}
	return 0; 
}
