#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#define ll long long
#define P 998244353
using namespace std;
const ll N=1e3+10;
ll a[N][N],t,id,cc,ff,f[N][N],f2[N][N],ansc,ansf,n,m;
char ch;
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%lld%lld",&t,&id);
	if(id==1) 
	{
		while(t--);
		printf("0 0\n");
		return 0;
	}
	if(id==5)
	{
		while(t--)
		{
			ansc=0;ansf=0;
			scanf("%lld%lld%lld%lld",&n,&m,&cc,&ff);
			for(ll i=1;i<=n;i++)
			{
		  	scanf("\n");
		  	for(ll j=1;j<=m;j++) scanf("%c",&ch);
			}
			for(int i=1;i<=n-2;i++)
			  ansc=(ansc+(n-i-1))%P;
			for(int i=3;i<=n-1;i++)
			  ansf=(ansf+(n-i)*(i-2))%P;
			
			ansc=(ansc*((m+1)/3))%P;
			ansf=(ansf*((m+1)/3))%P;
			
			printf("%lld %lld\n",ansc,ansf);
		}
		return 0;
	}
	if(id==6)
	{
		while(t--)
		{
			ansc=0;ansf=0;
			scanf("%lld%lld%lld%lld",&n,&m,&cc,&ff);
			for(ll i=1;i<=n;i++)
			{
		  	scanf("\n");
		  	for(ll j=1;j<=m;j++) scanf("%c",&ch);
			}
			ansc=(ansc+(m-1)*(m-1))%P;
			ansc=(ansc*((n+1)/4))%P;
			printf("%lld %lld\n",ansc,ansf);
		}
		return 0;
	}
	while(t--)
	{
		ansc=0;ansf=0;
		for(int i=1;i<=n;i++)
		  for(int j=1;j<=m;j++)
		    f[i][j]=f2[i][j]=0;
		scanf("%lld%lld%lld%lld",&n,&m,&cc,&ff);
		for(ll i=1;i<=n;i++)
		{
		  scanf("\n");
		  for(ll j=1;j<=m;j++)
		  {
		  	scanf("%c",&ch);
		  	if(ch=='0') a[i][j]=0;
		  	else a[i][j]=1;
		  	if(a[i][j]==1)
		  	{
		  		for(ll k=j-1;k>=1;k--)
		  		  if(a[i][k]==0)  f[i][k]=f[i][k+1]+1;
				else break;
				for(ll k=i-1;k>=1;k--)
				  if(a[k][j]==0) f2[k][j]=f2[k+1][j]+1;
				  else break;
			}
		  }  
		}
		for(ll i=1;i<=n;i++)
		{
			for(ll k=m;k>=1;k--)
			  if(a[i][k]==0) f[i][k]=f[i][k+1]+1;
			    else break;
		}
		for(ll j=1;j<=m;j++)
		{
			for(ll k=n;k>=1;k--)
			  if(a[k][j]==0) f2[k][j]=f2[k+1][j]+1;
			    else break;
		}

		for(ll j=1;j<=m;j++)
		{
			for(ll i=1;i<=n;i++)
			if(f2[i][j]>=2&&a[i][j]==0&&f[i][j]>1)
			{
				for(ll k=i+2;k<=i+f2[i][j]-1;k++)
				{
					ansc=(ansc+(f[i][j]-1)*(f[k][j]-1))%P;
					ansf=(ansf+(f[i][j]-1)*(f[k][j]-1)*(i+f2[i][j]-1-k))%P;
				}
			}
		}
		printf("%lld %lld\n",ansc*cc,ansf*ff);
	}
	return 0;
}