#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
int T,id,n,m,C,F,rht[1005][1005],dwn[1005][1005];
long long smrt[1005][1005],sf[1005][1005];
char a[1005][1005];
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	for(scanf("%d%d",&T,&id);T;T--)
	{
		scanf("%d%d%d%d",&n,&m,&C,&F);
		memset(rht,0,sizeof rht);
		memset(dwn,0,sizeof dwn);
		memset(smrt,0,sizeof smrt);
		memset(sf,0,sizeof sf);
		memset(a,0,sizeof a);
		long long tot=0;
		for(int ii=1;ii<=n;ii++)
		{
			scanf("%s",a[ii]+1);
			for(int i=m;i;i--)
				if(a[ii][i]!='1')
					rht[ii][i]=rht[ii][i+1]+1;
				else rht[ii][i]=0;
		}
		for(int i=n;i;i--)
		{
			for(int j=1;j<=m;j++)
				if(a[i][j]!='1')
					dwn[i][j]=dwn[i+1][j]+1;
				else dwn[i][j]=0;
		}
		for(int i=n;i;i--)
		{
			for(int j=1;j<=m;j++)
				if(a[i][j]!='1')
					smrt[i][j]=smrt[i+1][j]+rht[i][j]-1;
				else smrt[i][j]=0;
		}
		for(int i=n;i;i--)
		{
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]!='1')
					sf[i][j]=(sf[i+1][j]+max(0,rht[i][j]-1)*(dwn[i][j]-1))%Mod;
				else sf[i][j]=0;
			}
		}
//		for(int i=1;i<=n;i++)
//		{
//			for(int j=1;j<=m;j++)
//				printf("(%d,%d,%lld,%lld) ",rht[i][j],dwn[i][j],smrt[i][j],sf[i][j]);
//			puts("");
//		}
		long long CC=0,CF=0;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				if(dwn[i][j]<=2)continue;
				CC=(CC+smrt[i+2][j]*max(0,rht[i][j]-1))%Mod;
				CF=(CF+sf[i+2][j]*max(0,rht[i][j]-1))%Mod;
				//printf("i=%d j=%d CC=%d CF=%d\n",i,j,CC,CF);
			}
		printf("%d %d\n",C*CC,F*CF);
	}
	return 0;
}