#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--)
using namespace std;
const int maxn=1010,mod=998244353;
inline int read()
{
	int x=0,f=1;char c;
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-f;
	for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
int T,id;
int n,m,c,f,a[maxn][maxn];
int suml[maxn];
int len[maxn][maxn];
char s[maxn][maxn];
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),id=read();
	while(T--)
	{
		n=read(),m=read(),c=read(),f=read();
		For(i,1,n) scanf("%s",(s[i]+1));
		For(i,1,n) For(j,1,m) a[i][j]=s[i][j]^48;
		For(i,1,n)
		{
			int now=m;
			Rep(j,m,1)
			{
				if(a[i][j]) len[i][j]=0,now=j-1;
				else len[i][j]=now-j;
			}
		}
		int ansc=0,ansf=0;
		For(j,1,m)
		{
			int sum=0;
			For(i,1,n)
			{
				if(i>=3&&!a[i-1][j]&&!a[i-2][j]) 
					sum=sum+len[i-2][j];
				if(a[i][j]) sum=0;
				else ansc=(ansc+sum*len[i][j])%mod;
			}
		}
		For(j,1,m)
		{
			For(i,1,n) suml[i]=0;
			int now=n;
			Rep(i,n,1)
			{
				if(a[i][j]) suml[i]=0,now=i-1;
				else suml[i]=now-i;
			}
			int sum=0;
			For(i,1,n)
			{
				if(i>=3&&!a[i-1][j]&&!a[i-2][j])
					sum=sum+len[i-2][j];
				if(a[i][j]) sum=0;
				else ansf=(ansf+sum*len[i][j]%mod*suml[i]%mod)%mod;
			}
		}
		printf("%lld %lld\n",ansc*c,ansf*f);
	}
	return 0;
}