#include<bits/stdc++.h>
using namespace std;

const int maxn = 1005;
const int p = 998244353;
int T, id;
int n, m;
int a[maxn][maxn];
int s[maxn][maxn];

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	
	scanf("%d%d", &T, &id);
	while (T--) {
		scanf("%d%d", &n, &m);
		int VC, VF; scanf("%d%d", &VC, &VF);
		for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			scanf("%1d", &a[i][j]);
		
		for (int i = 1; i <= n; i++) {
			s[i][m + 1] = 0;
			for (int j = m; j; j--)
				s[i][j] = a[i][j] ? 0 : (s[i][j + 1] + 1);
		}
		for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			s[i][j] = max(s[i][j] - 1, 0);
		
		int SC = 0;
		for (int j = 1; j <= m; j++) {
			int tmp = 0;
			for (int i = n; i; i--) {
				if (a[i][j]) { tmp = 0; continue; }
				SC = (SC + 1LL * s[i][j] * tmp) % p;
				tmp = (tmp + s[i + 1][j]) % p;
			}
		}
		int SF = 0;
		for (int j = 1; j <= m; j++) {
			int tmp = 0, len = 0;
			for (int i = n; i; i--) {
				if (a[i][j]) { tmp = len = 0; continue; }
				SF = (SF + 1LL * s[i][j] * tmp) % p;
				len += 1;
				tmp = (tmp + 1LL * s[i + 1][j] * max(0, len - 2)) % p;
			}
		}
		printf("%lld %lld\n", 1LL * SC * VC % p, 1LL * SF * VF % p);
		
	}
}