#include<cstdio>
#include<cstring>
#define LL long long
//1.long long 2.1ll 3.%lld 4.xiabiao 5.MOD
using namespace std;
const long long MAXN=1005;
const long long MOD=998244353;
char ch;
inline long long rd()
{
	ch=getchar();
	long long ff=1ll,s=0;
	while(ch>'9' || ch<'0')
	{
		if(ch=='-')
			ff=-1ll;
		ch=getchar();
	}
	while(ch<='9' && ch>='0')
	{
		s=(s<<3)+(s<<1)+ch-48;
		ch=getchar();
	}
	return s*ff;
}
LL n,m,c,f,a[MAXN][MAXN],b[MAXN][MAXN],C[MAXN][MAXN],D[MAXN][MAXN],E[MAXN][MAXN],F[MAXN][MAXN],ansC,ansF;
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	long long T,id;
	T=rd();id=rd();
	while(T--)
	{
		n=rd();m=rd();c=rd()%MOD;f=rd()%MOD;
		ansC=0;ansF=0;
		memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(C,0,sizeof(C));
		memset(D,0,sizeof(D));memset(E,0,sizeof(E));memset(F,0,sizeof(F));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				ch=getchar();
				if(ch!='0' && ch!='1')ch=getchar();
				a[i][j]=ch&1;
			}
	
	
		for(int i=1;i<=n;i++)
			for(int j=m-1;j>=1;j--)
				b[i][j]=(a[i][j+1]||a[i][j])?0:b[i][j+1]+1;
		
		for(int i=3;i<=n;i++)
			for(int j=1;j<=m;j++)
				C[i][j]=(a[i-1][j])?0:(C[i-1][j]+b[i-2][j]);
		
		for(int i=3;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				D[i][j]=b[i][j]*C[i][j];
				ansC=(ansC+D[i][j])%MOD;
			}
		
		for(int i=n-1;i>=1;i--)
			for(int j=1;j<=m;j++)
			{
				E[i][j]=(a[i+1][j]||a[i][j])?0:E[i+1][j]+1;
				ansF=(ansF+E[i][j]*D[i][j])%MOD;
			}
			
	
	
	
		printf("%lld %lld\n",ansC*c%MOD,ansF*f%MOD);
							
							
	}
	return 0;
}