#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);++(i))
#define per(i,a,b) for(int (i)=(a);(i)>=(b);--(i))
using namespace std;
inline int read()
{
	int f=1,x=0; char c=getchar();
	while(!isdigit(c)) f=c=='-'?-1:1,c=getchar();
	while(isdigit(c)) x=(x<<3)+(x<<1)+(c-'0'),c=getchar();
	return f*x;
}
inline void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
	return void();
}
const int N=1e3+520,mod=998244353;
int T,id,n,m,A,B,sum[N][N];
char c[N][N];
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
    T=read(),id=read();
    while(T--)
    {
    	n=read(),m=read(),A=read(),B=read();
    	rep(i,1,n) rep(j,1,m) cin>>c[i][j];
    	rep(i,1,n)
    	{
    		sum[i][m+1]=0;
    		per(j,m,1) if(c[i][j]=='1') sum[i][j]=0;
    		else sum[i][j]=sum[i][j+1]+1;
		}
		int C=0,F=0;
		rep(j,1,m)
		{
			int res=0;
			rep(i,1,n)
			{
			    if(c[i][j]=='1')
				{
				    res=0;
				    continue;
				}
			    C=(C+res*(sum[i][j]-1)%mod)%mod;
				if(c[i-1][j]=='0') res=(res+sum[i-1][j]-1)%mod;	
			}
		}
		rep(j,1,m)
		{
			int res=0,Res=0;
			rep(i,1,n)
			{
			    if(c[i][j]=='1')
				{
				    res=Res=0;
				    continue;
				}
			    F=(F+Res)%mod;
				Res=(Res+res*(sum[i][j]-1)%mod)%mod;
				if(c[i-1][j]=='0') res=(res+sum[i-1][j]-1)%mod;	
			}
		}
		write(A*C%mod),putchar(' '),write(B*F%mod),putchar('\n');
	}
	return 0;
}
/*
2 0
4 3 1 1
001
010
000
000
6 6 1 1
000010
011000
000110
010000
011000
000000
*/