#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 998244353;
inline LL add (LL x, LL y) {
	return (x + y) % MOD;
}
inline LL mul (LL x, LL y) {
	return x * y % MOD;
}
LL ansc, ansf;
int mp[1005][1005], len[1005][1005], sum[1005];
void init () {
	memset(mp, 0, sizeof(mp));
	memset(len, 0, sizeof(len));
	memset(sum, 0, sizeof(sum));
	ansc = ansf = 0;
	return;
}
inline void getc (int col, int l, int r) {
	sum[l-1] = 0;
	for (int i = l; i <= r; i++) {
		sum[i] = add(sum[i-1], len[i][col]);
	}
	for (int i = l; i + 1 < r; i++) {
		ansc = add(ansc, mul(len[i][col], sum[r] - sum[i+1]));
	}
	return;
}
inline void getf (int col, int l, int r) {
	sum[l-1] = 0;
	for (int i = l; i <= r; i++) {
		sum[i] = add(sum[i-1], mul(len[i][col], r - i));
	}
	for (int i = l; i + 2 < r; i++) {
		ansf = add(ansf, mul(len[i][col], sum[r] - sum[i+1]));
	}
	return;
}
int main () {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T, id;
	scanf("%d%d", &T, &id);
	while (T--) {
		init();
		int n, m;
		LL c, f;
		scanf("%d%d%lld%lld", &n, &m, &c, &f);
		c = (c % MOD + MOD) % MOD;
		f = (f % MOD + MOD) % MOD;
		for (int i = 1; i <= n; i++) {
			char str[1005];
			scanf("%s", str);
			for (int j = 0; j < m; j++) {
				mp[i][j+1] = str[j] - '0';
			}
			for (int j = 1, k; j <= m;) {
				while (mp[i][j] && j <= m) {
					len[i][j] = -1;
					j++;
				}
				k = j;
				while (!mp[i][k] && k <= m) {
					k++;
				}
				for (; j < k; j++) {
					len[i][j] = k - j - 1;
				}
			}
		}
		for (int j = 1; j <= m; j++) {
			for (int i = 1, k; i <= m; i = k) {
				while (mp[i][j] && i <= n) {
					i++;
				}
				k = i;
				while (!mp[k][j] && k <= n) {
					k++;
				}
				if (i <= n + 1 && i < k - 1) {
					getc(j, i, k - 1);
					getf(j, i, k - 1);
				}
			}
		}
		ansc = (ansc % MOD + MOD) % MOD;
		ansf = (ansf % MOD + MOD) % MOD;
		printf("%lld %lld\n", mul(ansc, c), mul(ansf, f));
	}
	return 0;
}
