#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll ;
#define REP(i,j,k) for(int i=j;i<=k;++i)
inline int read(){
	int v=0,f=1; char c=getchar();
	while(c<48||57<c){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(48<=c&&c<=57) v=(v<<3)+(v<<1)+(c^48),c=getchar();
	return v*f;
}
inline ll readL(){
	ll v=0,f=1; char c=getchar();
	while(c<48||57<c){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(48<=c&&c<=57) v=(v<<3)+(v<<1)+(c^48),c=getchar();
	return v*f;
}
inline void print(int x){
	if(x>9) print(x/10);
	putchar(x%10+48);
}
inline void printL(ll x){
	if(x>9) printL(x/10);
	putchar(x%10+48);
}
inline void write(int x,char c){
	if(x<0) putchar('-'),x=-x;
	print(x),putchar(c);
}
inline void writeL(ll x,char c){
	if(x<0) putchar('-'),x=-x;
	printL(x),putchar(c);
}
const int N=1e3+5,p=998244353;
int n,m,c,f; ll rgt[N][N],dn[N][N],g[N][N],h[N][N],Ansc,Ansf; char s[N][N];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	for(int T=read(),id=read();T--;){
		n=read(),m=read(),c=read(),f=read();
		REP(i,1,n) scanf("%s",s[i]+1);
		memset(rgt,-1,sizeof rgt);
		REP(i,1,n)
			for(int j=m;j>=1;--j)
				if(s[i][j]=='0') rgt[i][j]=rgt[i][j+1]+1;
		memset(dn,-1,sizeof dn);
		REP(j,1,m)
			for(int i=n;i>=1;--i)
				if(s[i][j]=='0') dn[i][j]=dn[i+1][j]+1;
		memset(g,0,sizeof g);
		REP(j,1,m)
			for(int i=n;i>=1;--i)
				if(s[i][j]=='0') g[i][j]=(g[i+1][j]+rgt[i][j])%p;
		memset(h,0,sizeof h);
		REP(j,1,m)
			for(int i=n;i>=1;--i)
				if(s[i][j]=='0')
					h[i][j]=(h[i+1][j]+dn[i][j]*rgt[i][j]%p)%p;
		Ansc=Ansf=0;
		REP(i,1,n-2)
			REP(j,1,m)
				if(s[i][j]=='0'&&s[i+1][j]=='0'){
					(Ansc+=rgt[i][j]*g[i+2][j]%p)%=p;
					(Ansf+=rgt[i][j]*h[i+2][j]%p)%=p;
				}
		writeL(Ansc*c%p,' '),writeL(Ansf*f%p,'\n');
	}
}
