#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e3+5;
const int MOD = 998244353;
bool CYL;
int n,m;
ll V[5],c,f,R[MAXN][MAXN],S[MAXN][MAXN],D[MAXN][MAXN];
int A[MAXN][MAXN];
char str[MAXN];
bool Godd;
void Solve(int type)
{
	memset(S,0,sizeof S);memset(D,0,sizeof D);memset(R,0,sizeof R);
	for(int i=1;i<=n;++i)
		{
			if(A[i][m]) R[i][m]=m;
			else R[i][m]=m+1;
			for(int j=m-1;j>=1;--j)
			{
				if(A[i][j]) R[i][j]=j;
				else R[i][j]=R[i][j+1];
			}
		}
		for(int j=1;j<=m;++j)
		{
			if(A[n][j]) D[n][j]=n;
			else D[n][j]=n+1;
			
			if(R[n][j]>=j+2)
			{
				S[n][j]=R[n][j]-j-1;
				if(type) S[n][j]=0;
			}
			
			if(A[n][j]) S[n][j]=0;
			for(int i=n-1;i>=1;--i)
			{
				ll t=0;
				
				if(A[i][j]) D[i][j]=i;
				else D[i][j]=D[i+1][j];
				
				if(R[i][j]>=j+2) t=R[i][j]-j-1;
				
				if(!A[i][j])
				{
					if(D[i][j]>=i+3&&R[i][j]>=j+1)
					{
						V[type]=(V[type]+(S[i+2][j]-S[D[i][j]][j]+MOD)*t%MOD)%MOD;
					}
					
					if(type&&D[i][j]>=i+1) t=t*(D[i][j]-1ll*i-1ll)%MOD;
					S[i][j]=(S[i+1][j]+t)%MOD;
				}
			}
		}
}
int main()
{
//	cout<<1.0*(&Godd-&CYL)/1024/1024<<"MB"<<endl;
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;scanf("%d %d",&T,&id);
	while(T--)
	{
		memset(A,0,sizeof A);memset(V,0,sizeof V);
		scanf("%d %d %lld %lld",&n,&m,&c,&f);
		for(int i=1;i<=n;++i)
		{
			scanf("%s",str+1);
			for(int j=1;j<=m;++j)	A[i][j]=(str[j]=='1');
		}
		Solve(0);Solve(1);
		printf("%lld %lld\n",V[0]*c%MOD,V[1]*f%MOD);
	}
	return 0;
}
