#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=998244353;
inline int read()
{
	char C=getchar();
	int ANS=0,F=1;
	while (C<'0'||C>'9')
	{
		if (C=='-') F=-1;
		C=getchar();
	}
	while (C>='0'&&C<='9')
	{
		ANS=ANS*10+(C-'0');
		C=getchar();
	}
	return ANS*F;
}
inline int readchar()
{
	char C=getchar();
	while (C<'0'||C>'1') C=getchar();
	return C-'0';
}
int n,m;
const int N=1005;
int a[N][N],dp[N][N],w[N][N];
ll s[N][N];
void work()
{
	n=read(),m=read();
	int C=read(),F=read();
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++) a[i][j]=readchar();
	}
	for (int i=1;i<=n;i++)
	{
		dp[i][m+1]=0;
		for (int j=m;j>=1;j--)
		{
			if (a[i][j]==1) dp[i][j]=0;
			else dp[i][j]=dp[i][j+1]+1;
		}
	}
	for (int j=1;j<=m;j++)
	{
		w[n+1][j]=0;
		for (int i=n;i>=1;i--)
		{
			if (a[i][j]==1) w[i][j]=0;
			else w[i][j]=w[i+1][j]+1;
		}
	}
	for (int j=1;j<=m;j++)
	{
		s[0][j]=0;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j]==1) s[i][j]=0;
			else s[i][j]=(s[i-1][j]+dp[i][j]-1)%mod;
		}
	}
	ll ansc=0,ansf=0;
	for (int k=1;k<=m;k++)
	{
		for (int y=3;y<=n;y++)
		{
			if (a[y][k]==1||a[y-1][k]==1) continue;
			ansc=(ansc+1ll*(dp[y][k]-1)*s[y-2][k])%mod;
			ansf=(ansf+1ll*(dp[y][k]-1)*(w[y][k]-1)%mod*s[y-2][k])%mod;
		}
	}
	printf("%lld %lld\n",ansc*C,ansf*F);
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(),id=read();
	while (T--) work();
	return 0;
}
