#include<bits/stdc++.h>
#define N 1010
#define mod 998244353
#define ll long long
using namespace std;

int T,dd,n,m,c,f,ansC,ansF;
char a[N][N];
int g[N][N], h[N][N], sum[N], ss[N];

int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&dd);
	while(T--) {
		ansC=ansF=0;
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(int i = 1; i <= n; i++) scanf("%s", a[i]+1);
		for(int i = n; i >= 1; i--)
			for(int j = 1; j <= n; j++)
				if(a[i][j]=='0') h[i][j]=h[i+1][j]+1;
				else h[i][j]=0;
		for(int i = 1; i <= n; i++) {
			for(int j = m; j >= 1; j--) 
				if(a[i][j]=='0') g[i][j]=g[i][j+1]+1;
				else g[i][j]=0;
			for(int j = 1; j <= m; j++) if(a[i][j]=='0') {
				ansC=(ansC+(ll)sum[j]*(g[i][j]-1)%mod)%mod;
				if(h[i][j]>1) ansF=(ansF+(ll)(h[i][j]-1)*(g[i][j]-1)%mod*sum[j]%mod)%mod;
				if(i>1&&g[i-1][j]>1) sum[j]=((ll)sum[j]+g[i-1][j]-1)%mod;
			}
			else sum[j]=0;
		}
		memset(a, 0, sizeof(a)),memset(h,0,sizeof(h));
		memset(g,0,sizeof(g)),memset(sum,0,sizeof(sum));
		ansC*=c,ansF*=f;
		printf("%d %d\n", ansC, ansF);
	}
	return 0;
}