#include<iostream>
#include<cstdio>
#define int long long
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,l,r) for(int i=(l);i>=(r);i--)
using namespace std;
const int N=1010;
const int p=998244353;
int T,I;
int a[N][N];
int f[N][N];
int R[N][N];
int D[N][N];
int F[N][N];
char s[N][N];
int fR[N][N];
int n,m,cc,cf;
int MAX(int x,int y){return x>y?x:y;}
int calca(int x,int y,int X,int Y)
{
	if(x>X||y>Y) return 0;
	return f[X][Y]-f[x-1][Y]-f[X][y-1]+f[x-1][y-1];
}
int calcr(int x,int y,int X,int Y)
{
	if(x>X||y>Y) return 0;
	return fR[X][Y]-fR[x-1][Y]-fR[X][y-1]+fR[x-1][y-1];
}
int calcF(int x,int y,int X,int Y)
{
	if(x>X||y>Y) return 0;
	return F[X][Y]-F[x-1][Y]-F[X][y-1]+F[x-1][y-1];
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%lld%lld",&T,&I);
	while(T--)
	{
		scanf("%lld%lld%lld%lld",&n,&m,&cc,&cf);
		FOR(i,1,n) FOR(j,1,m) R[i][j]=D[i][j]=0;
		FOR(i,1,n) scanf("%s",s[i]+1);
		FOR(i,1,n) FOR(j,1,m) a[i][j]=s[i][j]-'0';
		FOR(i,1,n) FOR(j,1,m) f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
		FOR(i,1,n) ROF(j,m,1) R[i][j]=(a[i][j]?0:R[i][j+1]+1);
		ROF(i,n,1) FOR(j,1,m) D[i][j]=(a[i][j]?0:D[i+1][j]+1);
		FOR(i,1,n) FOR(j,1,m) R[i][j]=MAX(R[i][j]-1,0);
		FOR(i,1,n) FOR(j,1,m) D[i][j]=MAX(D[i][j]-1,0);
		FOR(i,1,n) FOR(j,1,m) fR[i][j]=((fR[i-1][j]+fR[i][j-1]-fR[i-1][j-1]+R[i][j])%p+p)%p;
		FOR(i,1,n) FOR(j,1,m) F[i][j]=(((F[i-1][j]+F[i][j-1]-F[i-1][j-1]+R[i][j]*D[i][j])%p+p)%p);
		int ans1=0,ans2=0;
		FOR(i,1,n-2) FOR(j,1,m)
		{
			int l=i,r=n+1;
			while(l<r)
			{
				int mid=(l+r)/2;
				if(!calca(i,j,mid,j)) l=mid+1;
				else r=mid;
			}
			ans1=(ans1+calcr(i+2,j,l-1,j)*R[i][j]%p)%p;
			ans2=(ans2+calcF(i+2,j,l-1,j)*R[i][j]%p)%p;
		}
		printf("%lld %lld\n",ans1*cc%p,ans2*cf%p);
	}
	return 0;
}
