#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD=998244353;
const int NUM=1005;
int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while('0'<=c&&c<='9')
	{
		x=x*10+(c-'0');
		c=getchar();
	}
	return x*f;
}
int t,n,m;
char a[NUM][NUM];
LL rr[NUM][NUM],dd[NUM][NUM];
LL dp[NUM][NUM],ansc,ansf;
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	LL c,f;
	t=read();
	read();
	while(t--)
	{
		ansc=ansf=0;
		n=read(),m=read(),c=read(),f=read();
		for(int i=1;i<=n;i++) cin>>a[i]+1;
//		for(int i=1;i<=n;i++)
//		{
//			for(int j=1;j<=m;j++) printf("%c",a[i][j]);
//			printf("\n");
//		}
		for(int i=1;i<=n;i++)
		{
			rr[i][m+1]=0;
			for(int j=m;j;j--)
			{
				if(a[i][j]=='1') rr[i][j]=0;
				else rr[i][j]=rr[i][j+1]+1;
			}
//			for(int j=1;j<=m;j++) printf("%lld ",rr[i][j]);
//			printf("\n");
		}
		for(int j=1;j<=m;j++)
		{
			dd[n+1][j]=0;
			for(int i=n;i;i--)
			{
				if(a[i][j]=='1') dd[i][j]=0;
				else dd[i][j]=dd[i+1][j]+1;
			}
		}
//		for(int i=1;i<=n;i++)
//		{
//			for(int j=1;j<=m;j++) printf("%lld ",dd[i][j]);
//			printf("\n");
//		}
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]=='0'&&a[i-1][j]=='0')
					dp[i][j]=(dp[i-1][j]+(rr[i-1][j]-1));
				else dp[i][j]=0;
				if(a[i][j]=='0')
				{
					ansc=(ansc+dp[i-1][j]*(rr[i][j]-1))%MOD;
					ansf=(ansf+dp[i-1][j]*(rr[i][j]-1)%MOD*(dd[i][j]-1))%MOD;
				}
			}
		}
		printf("%lld %lld\n",(ansc*c)%MOD,(ansf*f)%MOD);
	}
}
/*
feropen("plant.in","r",stdin);
freopen("plate.in","r",stdin);
freopen("plant.in","w",stdin);
freopen("plant1.ans","w",stdout);
ji le , kai bai
*/