#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod = 998244353;
const int MAXN = 1010;

int T,id,n,m,c,f,sum1[MAXN][MAXN],sum2[MAXN][MAXN];

inline void md(ll &x){
	if(x >= mod) x %= mod;
	return ;
}

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--){
		char mp[MAXN][MAXN];
		memset(sum1,0,sizeof(sum1));
		memset(sum2,0,sizeof(sum2));
		
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(int i = 0;i < n;i++)
			scanf(" %s",&mp[i]);
		if(c == 0 && f == 0){
			printf("0 0\n");
			continue;
		}
		
		for(int i = 0;i < n;i++){
			for(int j = m - 1;j >= 0;j--){
				if(mp[i][j] == '0'){
					sum1[i][j] = 1 + sum1[i][j + 1];
				}
				else {
					sum1[i][j] = 0;
				}
			}
		}
		if(f != 0){
			for(int j = 0;j < m;j++){
				for(int i = n - 1;i >= 0;i--){
					if(mp[i][j] == '0'){
						sum2[i][j] = 1 + sum2[i + 1][j];
					}
					else {
						sum2[i][j] = 0;
					}
				}
			}
		}
		
		ll Vc = 0,Vf = 0;
		for(int i = n - 1;i > 0;i--){
			for(int j = 0;j < n;j++){
				if(sum1[i][j] <= 1 || mp[i - 1][j] == '1') continue;
				ll tmp = 0;
				for(int k = i - 2;k >= 0;k--){
					if(mp[k][j] == '1') break;
					if(sum1[k][j] <= 1) continue;
					tmp += (sum1[k][j] - 1) * (sum1[i][j] - 1);
					md(tmp);	
				}
				Vc += tmp;
				md(Vc);
				if(mp[i + 1][j] == '0' && i + 1 < n){
					Vf += tmp * (sum2[i][j] - 1);
					md(Vf);
				}
			}
		}
		printf("%lld %lld\n",c * Vc,f * Vf);
	}
	return 0;
}
