#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1010,mod=998244535;
int n,m;char str[N];bool a[N][N];
int R[N][N],D[N][N];
int solvec()
{
	int ans=0;
	for(int j=1;j<=m;j++)
	{
		int res=0;
		for(int i=1;i<=n;i++)
		{
			if(!a[i][j]){res=0;continue;}
			ans=(ans+1ll*res*(R[i][j]-j))%mod;
			if(a[i-1][j]) res+=R[i-1][j]-j;
		}
	}
	return ans;
}
int solvef()
{
	int ans=0;
	for(int j=1;j<=m;j++)
	{
		int res=0;
		for(int i=1;i<=n;i++)
		{
			if(!a[i][j]){res=0;continue;}
			ans=(ans+1ll*res*(R[i][j]-j)%mod*(D[i][j]-i))%mod;
			if(a[i-1][j]) res+=R[i-1][j]-j;
		}
	}
	return ans;
}
void clear()
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) a[i][j]=R[i][j]=D[i][j]=0;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,c,f;
	scanf("%d%*d",&T);
	while(T --> 0)
	{
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(int i=1;i<=n;i++)
		{
			scanf("%s",str+1);
			for(int j=1;j<=m;j++) a[i][j]=str[j]=='0';
		}
		for(int i=1;i<=n;i++)
		{
			R[i][m+1]=m;
			for(int j=m;j;j--) if(a[i][j]) R[i][j]=R[i][j+1];
			else R[i][j]=j-1;
		}
		for(int j=1;j<=m;j++)
		{
			D[n+1][j]=n;
			for(int i=n;i;i--) if(a[i][j]) D[i][j]=D[i+1][j];
			else D[i][j]=i-1;
		}
		int vc=solvec(),vf=solvef();
		printf("%d %d\n",vc*c,vf*f);
		clear();
	}
	return 0;
}
