#include<bits/stdc++.h>
#define N 1005
using namespace std;
typedef long long ll;
const ll mod=998244353;
int T,id;
int c,f;
int n,m;
char a[N][N];
int r[N][N],d[N][N];
ll s[N][N],s2[N][N];
void workc()
{
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(d[i][j]>2&&r[i][j]>1)
				ans=(ans+1ll*(r[i][j]-1)*((s[j][i+d[i][j]-1]-s[j][i+1]+mod)%mod)%mod)%mod;
//			printf("i=%d j=%d d=%d s=%lld\n",i,j,d[i][j],s[j][i]);
		}
	}
	printf("%lld ",ans*c);
}
void workf()
{
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(d[i][j]>3&&r[i][j]>1)
				ans=(ans+1ll*(r[i][j]-1)*((s2[j][i+d[i][j]-2]-s2[j][i+1]+mod)%mod)%mod)%mod;
		}
	}
	printf("%lld\n",ans*f);
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--)
	{
		scanf("%d%d%d%d",&n,&m,&c,&f);
		memset(r,0,sizeof(r)),memset(d,0,sizeof(d));
		memset(s,0,sizeof(s)),memset(s2,0,sizeof(s2));
		for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
		for(int i=1;i<=n;i++)
		{
			for(int j=m;j>=1;j--)
			{
				if(a[i][j]=='0') r[i][j]=r[i][j+1]+1;
				else r[i][j]=0;
			}
		}
		for(int j=1;j<=m;j++)
		{
			for(int i=n;i>=1;i--)
			{
				if(a[i][j]=='0') d[i][j]=d[i+1][j]+1;
				else d[i][j]=0;
			}
		}
		for(int j=1;j<=m;j++)
		{
			for(int i=1;i<=n;i++)
			{
				s[j][i]=(s[j][i-1]+max(0,r[i][j]-1))%mod;
				s2[j][i]=(s2[j][i-1]+1ll*(max(0,d[i][j]-1)*max(0,r[i][j]-1)%mod))%mod;
			}
		}
		workc(),workf();
	}
	return 0;
}