#include<bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
const int MAXN=1000+10,mod=998244353,seed=499122177;
int T,id;
int n,m,c,f;
char a[MAXN][MAXN];
int lst[MAXN][MAXN],b[MAXN][MAXN],d[MAXN][MAXN];
int num[MAXN][MAXN],p[MAXN][MAXN];
void solve1()
{
	for(int i=1;i<=n;i++)
	{
		int t=m+1;
		for(int j=m;j>=1;j--)
		{
			if(a[i][j]=='1')t=j;
			lst[i][j]=t;
			d[i][j]=lst[i][j]*(i+1)%mod;
		}
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	num[i][j]=(num[i-1][j]+lst[i][j])%mod,p[i][j]=(p[i-1][j]+d[i][j])%mod;
}
void solve2()
{
	for(int j=1;j<=m;j++)
	{
		int t=n+1;
		for(int i=n;i>=1;i--)
		{
			if(a[i][j]=='1')t=i;
			b[i][j]=t;
		}
	}
}
int query1()
{
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]=='1'||a[i+1][j]=='1'||i>=n-1||j==m)continue;
			sum=(sum+(num[b[i][j]-1][j]-num[i+1][j]-(j+1)*(b[i][j]-1-i-1))*(lst[i][j]-j-1)%mod)%mod;
		}
	}
	return sum;
}
int query2()
{
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]=='1'||a[i+1][j]=='1'||a[i+2][j]=='1'||i>=n-2||j==m)continue;
			int t=0;
			t=(t+(num[b[i][j]-2][j]-num[i+1][j])*b[i][j]%mod)%mod;
			t=(t-(p[b[i][j]-2][j]-p[i+1][j]+mod)%mod+mod)%mod;
			t=(t-b[i][j]*(j+1)%mod*(b[i][j]-i-3)%mod)%mod;
			t=(t+(j+1)*(b[i][j]+i+2)%mod*(b[i][j]-i-3)%mod*seed%mod)%mod;
			sum=(sum+t*(lst[i][j]-j-1)%mod)%mod;
		}
	}
	return sum;
}
signed main()
{
	freopen("plant.in","r",stdin);freopen("plant.out","w",stdout);
	T=read();id=read();
	while(T--)
	{
		n=read();m=read();c=read();f=read();
		for(int i=1;i<=n;i++)
		scanf("%s",a[i]+1);
		solve1();
		solve2();
		printf("%lld %lld\n",query1()*c%mod,query2()*f%mod);
		for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)lst[i][j]=b[i][j]=d[i][j]=num[i][j]=p[i][j]=0;
	}
	return 0;
}
