#include <bits/stdc++.h>

#define ll long long
#define N 1005
using namespace std;

int T,id;
int n,m,c,f;
ll resc,resf;
short int tf[N][N];

ll read()
{
    ll x=0;
	int f=1;
    char ch=0;
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+(ch-'0');
        ch=getchar();
    }
    return x*f;
}

int anoread()
{
	char ch=getchar();
	while(ch=='\n')
		ch=getchar();
	return ch-'0';
}

void judge(int x,int y)
{
	int i,j,ktop=0,ktail=0;
	for(i=x;i<n;i++)
	{
		if(tf[i][y]==1)
			break;
	}
	ktail=i-1;
	for(j=y;j<m;j++)
	{
		if(tf[x][j]==1)
			break;
	}
	ktop=j-y-1;
	
	for(i=x+2;i<=ktail;i++)
	{
		if(tf[i][y]==1)
			break;
		for(j=y;j<m;j++)
		{
			if(tf[i][j]==1)
				break;
		}
		resc+=ktop*(j-y-1);
		resf+=ktop*(j-y-1)*(ktail-i);
	}
}

void perf()
{
	int i,j;
	for(i=0;i<n-2;i++)
	{
		for(j=0;j<m-1;j++)
		{
			if(tf[i][j]==1)
				continue;
			judge(i,j);
		}
	}
	
}

//998244353
int main()
{
	freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
    T=read();
    id=read();
    int iiiii,i,j;
    for(iiiii=1;iiiii<=T;iiiii++)
    {
    	resc=0;
    	resf=0;
    	n=read();
    	m=read();
    	c=read();
    	f=read();
    	if(id==1)
    		printf("0 0\n");
    	else if(id==2)
    	{
    		short int ppp[6]={0};
    		for(i=0;i<6;i++)
    			ppp[i]=read();
    		resf=0;
    		if(ppp[0]==1||ppp[2]==1||ppp[4]==1||ppp[1]==1||ppp[5]==1)
   				resc=0;
   			else
    			resc=1;
    		printf("%lld %lld\n",resc,resf);
		}
		else if(id==3)
    	{
    		short int ppp[8]={0};
    		for(i=0;i<8;i++)
    			ppp[i]=read();
    		if(ppp[0]==1||ppp[1]==1||ppp[2]==1||ppp[4]==1||ppp[5]==1||ppp[6]==1)
   				resf=0;
   			else
    			resf=1;
    		if(ppp[0]==0&&ppp[1]==0&&ppp[2]==0&&ppp[4]==0&&ppp[5]==0)
   				resc++;
   			if(ppp[2]==0&&ppp[3]==0&&ppp[4]==0&&ppp[6]==0&&ppp[7]==0)
   				resc++;
   			if(ppp[0]==0&&ppp[1]==0&&ppp[2]==0&&ppp[4]==0&&ppp[6]==0&&ppp[7]==0)
   				resc++;
    		printf("%lld %lld\n",resc,resf);
		}
		
		else
		{
			for(i=0;i<n;i++)
			{
				for(j=0;j<m;j++)
				{
					tf[i][j]=anoread();
				}
			}
			perf();
			printf("%d %d\n",(c*resc)%998244353,(f*resf)%998244353);
		}
	}
    
    return 0;
}