#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAXN 1050
#define ll long long
using namespace std;
inline int read()
{
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	return c-'0';
}
const int mod=998244353;
int yard[MAXN][MAXN],n,m;
int xsort[MAXN][MAXN],xsum[MAXN][MAXN],ysort[MAXN][MAXN],xysum[MAXN][MAXN];
void yardread()
{
	memset(yard,0,sizeof yard);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			yard[i][j]=read();
		}
	}
}
void init()
{
	memset(ysort,0,sizeof ysort);
	memset(xsum,0,sizeof xsum);
	memset(xsort,0,sizeof xsort);
	memset(xysum,0,sizeof xysum);
	int k=0;
	for(int i=1;i<=n;i++)
	{
		k=0;
		for(int j=m;j>=1;j--)
		{
			if(yard[i][j] == 1){
				xsort[i][j]=-1;	k=0;
			}
			else
			{
				xsort[i][j]=k;k++;
			}
		}
	}
	for(int j=m;j>=1;j--)
	{
		k=0;
		for(int i=n;i>=1;i--)
		{
			if(yard[i][j] == 1){
				xysum[i][j]=xsum[i][j]=ysort[i][j]=-1;	k=0;
			}
			else
			{
				ysort[i][j]=k;k++;
				xsum[i][j]=xsort[i][j];
				xysum[i][j]=xsort[i][j]*ysort[i][j];
				if(yard[i+1][j] == 0)
				{
					xysum[i][j]+=xysum[i+1][j];
					xsum[i][j]+=xsum[i+1][j];	
				}
			}
		}
	}
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	long long c,f,sumf,sumc;
	scanf("%d %d",&T,&id);
	for(int Ti=1;Ti<=T;Ti++)
	{
		scanf("%d %d %lld %lld",&n,&m,&c,&f);
		yardread();
		init();
		ll sumf=0,sumc=0;
		for(int j=1;j<=m;j++)
		{
			for(int i=1;i<=n;i++)
			{
				while(yard[i][j] == 1) i++;
				if(ysort[i][j]>=2){
					sumc+=xsort[i][j]*xsum[i+2][j];
					sumc%=mod;	
				}
				if(ysort[i][j]>=3)
				{
					sumf+=xsort[i][j]*xysum[i+2][j];
					sumc%=mod;
				}
			}
		}
		sumf%=mod;sumc%=mod;
		printf("%lld %lld\n",c*sumc,f*sumf);
	}
	return 0;
}
