#include<bits/stdc++.h>
#define int long long
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rep(i,a,b) for(int i=a;i>=b;i--)
#define p(x) printf("%lld ",(x))
#define P() puts("")
using namespace std;


inline int read()
{
	char c=getchar();int x=0;bool f=0;
	while(!isdigit(c)) f^=!(c^45),c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;
	return x;
}

inline int diread()
{
	char c=getchar();int x=0;
	while(!isdigit(c)) c=getchar();
	x=(c^48);
	return x;
}

const int maxn=1050;
const int mod=998244353;

int T,Id;
int n,m,xc,xf;
int ansc,ansf;
int c,cc,ccc,f,ff,fff;
int ys;
int a[maxn][maxn],b[maxn][maxn];

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read();Id=read();
	
	while(T--)
	{
		n=read();m=read();xc=read();xf=read();
		For(i,1,n)
			For(j,1,m)
				a[i][j]=diread();
		For(i,1,n)
		{
			b[i][m]=0;
			Rep(j,m-1,1)
			{
				if(a[i][j])
					b[i][j]=0;
				else 
				{
					if(a[i][j+1])
						b[i][j]=0;
					else
						b[i][j]=b[i][j+1]+1;
				}
			}
		}
		
		//For(i,1,n)
		//{
		//	For(j,1,m)
		//		p(b[i][j]);
		//	P();
		//}
		
		ansc=0;ansf=0;
		c=0;cc=0;ccc=0;
		f=0;ff=0;fff=0;
		ys=0;
		For(j,1,m)
			a[n+1][j]=1;
		For(j,1,m)
		{
			c=0;cc=0;ccc=0;
			f=0;ff=0;fff=0;
			ys=0;
			Rep(i,n,1)
			{
				c+=cc;cc=ccc;ccc=0;
				f+=ff;ff=fff;fff=0;
				
				//p(i);p(j);P();
				//p(c);p(cc);p(ccc);P();
				//p(f);p(ff);p(fff);P();
				
				if(a[i][j]==1)
				{
					c=0;cc=0;
					f=0;ff=0;
					ys=0;
					continue;
				}
				if(a[i+1][j]==0) ys++;
				ccc+=b[i][j];
				fff+=b[i][j]*ys;
				ansc=(ansc+b[i][j]*c)%mod;
				ansf=(ansf+b[i][j]*f)%mod;
			}
		}
		ansc=(ansc*xc)%mod;
		ansf=(ansf*xf)%mod;
		p(ansc);p(ansf);P();
	}
	return 0;
}
