#include<bits/stdc++.h>
#define ll long long 

using namespace std;
const ll mod=998244353;
const ll N=1010;
ll fac[N],ifac[N];
ll qpow(ll _,ll __) //_^__
{
	ll ___=1;
	while(__)
	{
		if(__&1) ___=(___*_)%mod;
		_=(_*_)%mod;
		__>>=1;
	}
	return ___;
}
inline ll C(int n,int m)
{
	return fac[n]*ifac[n-m]%mod*ifac[m]%mod;
}
int debug=0;
int n,m,c,f;
int a[N][N];
int heng[N][N];// ***** 
int shu[N][N];
int sumh[N][N];
int cc[N][N];
ll vc,vf;
inline int summ(int x_,int y_,int x__,int y__)
{
	if(x_>x__||y_>y__) return 0;
	return sumh[x__][y__]-sumh[x_-1][y__]-sumh[x__][y_-1]+sumh[x_-1][y_-1];
}
inline ll sumd(int n) //1+2+...+n
{
	return (1ll*n*(n+1)/2)%mod;
}
int mian()
{
	scanf("%d%d%d%d",&n,&m,&c,&f);
	vc=vf=0;
	memset(heng,0,sizeof(heng));
	memset(shu,0,sizeof(shu));
	memset(sumh,0,sizeof(sumh));
	memset(cc,0,sizeof(cc));
	for(int i=1;i<=n;i++) 
	{
		char s[N];
		scanf("%s",s+1);
		for(int j=1;j<=m;j++)
		{
			a[i][j]=s[j]=='0'? 0:1;
		}
	}
	
	for(int i=1;i<=n;i++) for(int j=m;j>=1;j--)
	{
		if(a[i][j]==1) heng[i][j]=0;
		else heng[i][j]=heng[i][j+1]+1;
	}
	for(int i=1;i<=n;i++) for(int j=m;j>=1;j--)
	{
		heng[i][j]=heng[i][j]-1;
		if(heng[i][j]<0) heng[i][j]=0;
	}
	for(int i=n;i>=1;i--) for(int j=1;j<=m;j++)
	{
		if(a[i][j]==1) shu[i][j]=0;
		else shu[i][j]=shu[i+1][j]+1;
	}
	
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
	{
		sumh[i][j]=sumh[i-1][j]+sumh[i][j-1]-sumh[i-1][j-1]+heng[i][j];
	}
	if(debug)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				printf("%d ",heng[i][j]);
			}
			puts("");
		}
	}
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
	{
		cc[i][j]=(heng[i][j]*(summ(i+2,j,i+shu[i][j]-1,j)%mod))%mod;
	}
	if(debug)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				printf("%d ",cc[i][j]);
			}
			puts("");
		}
	}
	if(c)
	{
		for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
		{
			vc=(vc+cc[i][j])%mod;
		}
	}
	if(f)
	{
		for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=i+2;k<=i+shu[i][j]-1;k++)
		{
			vf=(vf+heng[i][j]*heng[k][j]*(shu[k][j]-1))%mod;
		}
	}
	printf("%d %d\n",vc*c,vf*f);
	return 0;
}

int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	scanf("%d%d",&T,&id);
	if(id<=4&&id>=1)
	{
		while(T--)
		{
			printf("0 0\n");
		}
	}
	fac[1]=ifac[1]=1;
	for(int i=2;i<=1001;i++)
	{
		fac[i]=(fac[i-1]*i)%mod;
		ifac[i]=qpow(fac[i],mod-2);
	}
	while(T--)
	{
		mian();
	}
	
	
	return 0;
}
