#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return x*f;
}
const ll N=1010,mod=998244353,G=3;
ll T,ID,n,m,a[N][N],b[N][N],c[N][N],ans1,ans2,C,F;
char str[N][N];
void work(){
	n=read(),m=read(),C=read(),F=read();
	ans1=ans2=0;
	for(ll i=1;i<=n;i++)scanf("%s",str[i]+1);
	for(ll i=1;i<=n;i++)
		for(ll j=1;j<=m;j++)
			a[i][j]=str[i][j]-'0';
	for(ll i=1;i<=n;i++)
		for(ll j=1;j<=m;j++)b[i][j]=c[i][j]=0;
	for(ll i=1;i<=n;i++)
		for(ll j=m;j;j--)
			b[i][j]=a[i][j]?0:b[i][j+1]+1;
	for(ll j=1;j<=m;j++)
		for(ll i=n;i;i--)
			c[i][j]=a[i][j]?0:c[i+1][j]+1;
	for(ll J=1,l,r;J<=m;J++){
		l=1,r=l+c[l][J]-1;
		while(r<=n){
			for(ll i=l;i<=r;i++)
				if(b[i][J])
					for(ll j=i+2;j<=r;j++)
						if(b[j][J])
							ans1=(ans1+(b[i][J]-1)*(b[j][J]-1)%mod)%mod;
			l=r+1;
			while(a[l][J])l++;
			if(l>=n)break;r=l+c[l][J]-1;
		}
	}
	for(ll J=1,l,r;J<=m;J++){
		l=1,r=l+c[l][J]-1;
		while(r<=n){
			for(ll i=l;i<=r;i++)
				if(b[i][J])
					for(ll j=i+2;j<=r;j++)
						if(b[j][J])
							ans2=(ans2+(b[i][J]-1)*(b[j][J]-1)%mod*(r-j)%mod)%mod;
			l=r+1;
			while(a[l][J])l++;
			if(l>=n)break;r=l+c[l][J]-1;
		}
	}
	ans1=(ans1*C)%mod,ans2=(ans2*F)%mod;
	printf("%lld %lld\n",ans1,ans2);
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),ID=read();
	while(T--)work();
	return 0;
}