#include<bits/stdc++.h>
using namespace std;
#define N 1005
#define Mod 998244353
#define Max(x,y)((x)>(y)?x:y)
#define For(i,x,y)for(i=x;i<=(y);i++)
#define Down(i,x,y)for(i=x;i>=(y);i--)
char a[N][N];
int len[N],suf[N];
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id,n,m,c,f,i,j,totc,totf,pre;
	scanf("%d%d",&T,&id);
	while(T--)
	{
		scanf("%d%d%d%d",&n,&m,&c,&f);
		For(i,1,n)scanf("%s",a[i]+1),len[i]=0;
		suf[n+1]=totc=totf=0;
		Down(j,m,1)
		{
			Down(i,n,1)suf[i]=(a[i][j]=='1'?0:suf[i+1]+1);
			pre=0;
			For(i,1,n)
			{
				len[i]=(a[i][j]=='1'?0:len[i]+1);
				if(i>2)pre=(pre+Max(len[i-2]-1,0))%Mod;
				if(a[i-1][j]=='1')pre=0;
				totc=(totc+1LL*Max(len[i]-1,0)*pre)%Mod;
				totf=(totf+1LL*Max(len[i]-1,0)*pre%Mod*Max(suf[i]-1,0))%Mod;
			}
		}
		printf("%d %d\n",totc*c,totf*f);
	}
	return 0;
}
