#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mod=998244353;
const int N=1000+10;
int n,m,c,f;
int mp[N][N];
int sumh[N][N];
int suml[N][N];
int pos[N][N];
int down[N][N]; 
ll countC()
{
	ll ans=0;
	for(int y=1;y<m;y++)
	{
		for(int xa=1;xa<=n;xa++)
		{
			for(int xb=xa+2;xb<=n;xb++)
			{
				if(suml[xb][y]-suml[xa-1][y]!=0) break;
				ans=(ans+(ll)(pos[xa][y]-y-1)*(pos[xb][y]-y-1))%mod;
			}
		}
	}
	return ans%mod;
}
ll countF()
{
	ll ans=0;
	for(int y=1;y<m;y++)
	{
		for(int xa=1;xa<=n;xa++)
		{
			for(int xb=xa+2;xb<n;xb++)
			{
				if(suml[xb][y]-suml[xa-1][y]!=0) break;
				ans=(ans+(ll)(down[xb][y]-xb-1)*(pos[xa][y]-y-1)%mod*(pos[xb][y]-y-1)%mod)%mod;
			}
		}
	}
	return ans%mod;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	scanf("%d%d",&T,&id);
	while(T--)
	{
		scanf("%d%d%d%d",&n,&m,&c,&f);
		memset(mp,0,sizeof(mp));
		memset(pos,0,sizeof(pos));
		memset(suml,0,sizeof(suml));
		memset(sumh,0,sizeof(sumh));
		memset(down,0,sizeof(down));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				char ch=getchar();
				while(ch != '0' && ch != '1')
				{
					ch=getchar();
				}
				mp[i][j]=ch-'0';
				sumh[i][j]=sumh[i][j-1]+mp[i][j];
				suml[i][j]=suml[i-1][j]+mp[i][j];
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(mp[i][j]==0 && j!=1 && mp[i][j-1]==0)
				{
					pos[i][j]=pos[i][j-1];
					continue;
				}
				if(mp[i][j]==1)
				{
					pos[i][j]=j;
					continue;
				}
				for(int k=j+1;k<=m;k++)
				{
					if(mp[i][k]==1)
					{
						pos[i][j]=k;
						break;
					}
				}
				if(pos[i][j]==0) pos[i][j]=m+1;
			}
		}
		for(int j=1;j<=m;j++)
		{
			for(int i=1;i<=n;i++)
			{
				if(mp[i][j]==0 && i!=1 && mp[i-1][j]==0)
				{
					down[i][j]=down[i-1][j];
					continue;
				}
				if(mp[i][j]==1) 
				{
					down[i][j]=i;
					continue;
				}
				for(int k=i+1;k<=n;k++)
				{
					if(mp[k][j]==1)
					{
						down[i][j]=k;
						break;
					}
				}
				if(down[i][j]==0) down[i][j]=n+1;
			}
		}
		if(id==1)
		{
			printf("0 0\n");
			continue;
		}
		ll resC=countC();
		resC*=c;
		ll resF=0;
		if(f!=0) resF=countF();
		resF*=f;
		printf("%lld %lld\n",resC,resF);
	}	
	return 0;
}