#include<bits/stdc++.h>
#define int long long
#define F(i,j,k) for(register int i=j,_End=k;i<=_End;i++)
#define D(i,j,k) for(register int i=j,_End=k;i>=_End;i--)
#define N 1005
#define mod 998244353
using namespace std;

inline void ri(int &x)
{
	x=0;
	bool f=0;
	char c=getchar();
	while((c<48||c>57)&&c!='-')	c=getchar();
	if(c=='-')	f=1,c=getchar();
	while(c>47&&c<58)	x=x*10+c-48,c=getchar();
	if(f)	x=-x;
}

int T,id,n,m,c,f,ansc,ansf,a[N][N],sx[N][N],sy[N][N],tx[N][N],ty[N][N];

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ri(T),ri(id);
	while(T--)
	{
		ansc=ansf=0;
		F(i,0,1e3)	F(j,0,1e3)	a[i][j]=sx[i][j]=sy[i][j]=tx[i][j]=ty[i][j]=0;
		ri(n),ri(m),ri(c),ri(f);
		F(i,1,n)
			F(j,1,m)
				scanf("%1lld",&a[i][j]);
		F(i,1,n)	D(j,m,1)
		{
			if(a[i][j]==1)	sx[i][j]=0;
			else	sx[i][j]=(sx[i][j+1]+1)%mod;
		}
		F(j,1,m)	D(i,n,1)
		{
			if(a[i][j]==1)	sy[i][j]=0;
			else	sy[i][j]=(sy[i+1][j]+1)%mod;
		}
		F(j,1,m)	D(i,n,1)
		{
			if(a[i][j]==1)	ty[i][j]=0;
			else	ty[i][j]=((ty[i+1][j]+sx[i][j]-1)%mod+mod)%mod;
		}
		F(j,1,m)	D(i,n,1)
		{
			if(a[i][j]==1)	tx[i][j]=0;
			else	tx[i][j]=((tx[i+1][j]+(sx[i][j]-1)*(sy[i][j]-1))%mod+mod)%mod;
		}
		F(i,1,n)
			F(j,1,m)
			{
				if(a[i][j]||i+2>n||a[i+1][j]==1)	continue;
				ansc=((ansc+ty[i+2][j]*(sx[i][j]-1))%mod+mod)%mod;
			}
		F(i,1,n)
			F(j,1,m)
			{
				if(a[i][j]||i+2>n||a[i+1][j]==1)	continue;
				ansf=((ansf+tx[i+2][j]*(sx[i][j]-1))%mod+mod)%mod;
			}
		printf("%lld %lld\n",(ansc*c%mod+mod)%mod,(ansf*f%mod+mod)%mod);
	}
	return 0;
}
