#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int T,id,n,m;
int a[N][N],sum[N][N],h[N][N],l[N][N],hsum[N][N],lmax[N][N];
const long long mo=998244353;
void pre2()
{
	int i,j;
	for (j=1;j<=m;j++)
	  for (i=1;i<=n;i++)
	    sum[i][j]=sum[i-1][j]+a[i][j];
	for (i=1;i<=n;i++)
	  for (j=m;j>=1;j--) 
	  {
	  	if (a[i][j]==0) h[i][j]=h[i][j+1]+1;
	  	else h[i][j]=0;
	  }
	for (j=1;j<=m;j++)
	  for (i=n;i>=1;i--)
	  {
	  	if (a[i][j]==0) l[i][j]=l[i+1][j]+1;
	  	else l[i][j]=0;
	  }
	memset(hsum,0,sizeof(hsum));
	memset(lmax,0,sizeof(lmax));
	for (j=1;j<=m;j++)
	  for (i=n;i>=1;i--)
	    hsum[i][j]=hsum[i+1][j]+h[i][j];
	for (j=1;j<=m;j++)
	  for (i=n;i>=1;i--)
	  {
	  	if (a[i][j]==0) 
		  {if (a[i+1][j]==0)
		    lmax[i][j]=max(lmax[i+1][j],i);
		    else lmax[i][j]=i;
		}
	  	else lmax[i][j]=-1;
	  }
}
void pre()
{
	int i,j;
	for (j=1;j<=m;j++)
	  for (i=1;i<=n;i++)
	    sum[i][j]=sum[i-1][j]+a[i][j];
	for (i=1;i<=n;i++)
	  for (j=m;j>=1;j--) 
	  {
	  	if (a[i][j]==0) h[i][j]=h[i][j+1]+1;
	  	else h[i][j]=0;
	  }
	for (j=1;j<=m;j++)
	  for (i=n;i>=1;i--)
	  {
	  	if (a[i][j]==0) l[i][j]=l[i+1][j]+1;
	  	else l[i][j]=0;
	  }
}
void solve1(int c,int f)
{
	int p,q,k;
	long long ansc=0,ansf=0;
	pre();
	for (p=1;p<=n-2;p++)
	  for (q=p+2;q<=n;q++)
	    for (k=1;k<m;k++)
	      if (sum[q][k]-sum[p-1][k]==0)
	      {
	      	long long t=1ll*max(0,h[p][k]-1)*max(0,h[q][k]-1)%mo;
	      	ansc=(ansc+t)%mo;
	      	ansf=(ansf+t*(1ll*max(0,l[q][k]-1))%mo)%mo;
		  }
	printf("%lld %lld\n",1ll*c*ansc,1ll*f*ansf);
}
void solve2(int c,int f)
{
	pre2();
	int p,q,k;
	long long ansc=0,ansf=0;
	pre();
	for (p=1;p<=n-2;p++)
	    for (k=1;k<m;k++)
	    {
	      int q=lmax[p][k];
	      if (q<=p+1) continue;    
	      long long t=1ll*(h[p][k]-1)*(hsum[p+2][k]-hsum[q+1][k]-(q-p-1));
	      ansc=(ansc+t)%mo;
		}
	printf("%lld 0\n",1ll*c*ansc);
}
void solve3()
{
//	puts("iloveccf");
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int c,f,i,j;
	char s[1005];
	scanf("%d%d",&T,&id);
	while (T--)
	{	bool flagf=true;
	    bool flagc=true;
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for (i=1;i<=n;i++)
		{
			scanf("%s",s);
			for (j=0;j<m;j++) 
			{
				if (i%4==0&&s[j]!='1') flagf=false; 
				if ((j+1)%3==0&&s[j]!='1') flagc=false;
				if (s[j]=='0') a[i][j+1]=0;
				else a[i][j+1]=1;
			}
		}
		if (c==0&&f==0) 
		{
			printf("%d %d\n",0,0);
			continue;
		}
		if (n*n*m<=10000000) 
		{
			solve1(c,f);
			continue;
		}
		if (flagf) f=0;
		if (f==0)
		{
			solve2(c,f);
			continue;
		}
		if (flagc)
		{
			solve3();
			continue;
		}
	}
	return 0;
}
