#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
int T,n,m;
ll end1[1005][1005],end2[1005][1005],sum[1005][1005],c,f,tot[1005][1005],id;
char s[1005][1005];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d", &T, &id);
	while(T--){
		memset(tot,0,sizeof(tot));
		memset(sum,0,sizeof(sum));
		memset(end1,0,sizeof(end1));
		memset(end2,0,sizeof(end2));
		scanf("%d%d%lld%lld", &n, &m, &c, &f);
		if(c==0 && f==0){
			puts("0 0");
			continue;
		}
		for(int i = 1; i <= n; i++){
			scanf("%s", s[i]+1);
			end2[i][m]=m+1;
			for(int j = m; j; j--)
				if(s[i][j]=='1') end2[i][j]=j;
				else if(j!=m) end2[i][j]=end2[i][j+1];
		}
		for(int j = 1; j <= m; j++){
			end1[n][j]=n+1;
			for(int i = n; i; i--)
				if(s[i][j]=='1') end1[i][j]=i;
				else if(i!=n) end1[i][j]=end1[i+1][j];
		}
		for(int j = 1; j <= m; j++)
			for(int i = n; i; i--)
				if(s[i][j]=='0') sum[i][j]=(sum[i+1][j]+end2[i][j]-j-1+mod)%mod;
		for(int i = n; i; i--)
			for(int j = m; j; j--)
				if(s[i][j]=='0') tot[i][j]=(tot[i+1][j]+(end1[i][j]-i-1+mod)%mod*(end2[i][j]-j-1+mod)%mod%mod)%mod;//, printf("%d %d %lld\n", i,j,(end1[i][j]-i-1+mod)%mod*(end2[i][j]-j-1+mod)%mod%mod);
		ll retc=0,retf=0;
		for(int i = 1; i <= n-2; i++)
			for(int j = 1; j < m; j++){
				if(s[i][j]=='0' && end1[i][j]>i+2){
					retc=(retc+(end2[i][j]-j-1+mod)%mod*sum[i+2][j]%mod)%mod;
					if(end1[i][j]>i+3){
						//printf("%d %d %lld\n", i,j,(end2[i][j]-j-1+mod)%mod*tot[i+2][j]%mod);
						retf=(retf+(end2[i][j]-j-1+mod)%mod*tot[i+2][j]%mod)%mod;
					}
				}
			}
		printf("%lld %lld\n", retc*c%mod, retf*f%mod);
	}
	return 0;
}