#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010, mod = 998244353;
#define ll long long
int n, m, c_num, f_num;
char c[maxn][maxn];
int a[maxn][maxn], b[maxn][maxn], sumc[maxn][maxn];
ll sumf[maxn][maxn];
inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (!isdigit(c)) {
		if (c == '-') f = -1;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();
	}
	return x * f;
}
void solve() {
	n = read(), m = read(), c_num = read(), f_num = read();
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf(" %c", &c[i][j]);
		}
	}
	// a heng b shu
	for (int i = 1; i <= n; i++) {
		a[i][m+1] = 0;
		for (int j = m; j >= 1; j--) {
			if (c[i][j] == '0') a[i][j] = a[i][j+1] + 1;
			else a[i][j] = 0;
		}
	}
	for (int j = 1; j <= m; j++) {
		b[n+1][j] = 0;
		for (int i = n; i >= 1; i--) {
			if (c[i][j] == '0') b[i][j] = b[i+1][j] + 1;
			else b[i][j] = 0;
		}
	}
	memset(sumc, 0, sizeof(sumc));
	memset(sumf, 0, sizeof(sumf));
	for (int j = 1; j <= m; j++) {
		for (int i = n; i >= 1; i--) {
			if (c[i][j] == '0') sumc[i][j] = sumc[i+1][j] + a[i][j] - 1;
			else sumc[i][j] = 0;
		}
	}
	for (int j = 1; j <= m; j++) {
		for (int i = n; i >= 1; i--) {
			if (c[i][j] == '0') sumf[i][j] = sumf[i+1][j] + (a[i][j]-1) * (b[i][j]-1);
			else sumf[i][j] = 0;
		}
	}
	ll ans_c = 0, ans_f = 0;
	for (int i = 1; i <= n-2; i++) {
		for (int j = 1; j <= m; j++) {
			if (c[i][j] == '1' || c[i+1][j] =='1') continue;
			ans_c = (ans_c + (ll)(a[i][j]-1) * (ll)sumc[i+2][j] % mod) % mod;
		//	cout << i << " " << j << " " << (ll)(a[i][j]-1) * (ll)sumc[i+2][j] << endl;
			ans_f = (ans_f + (ll)(a[i][j]-1) * (ll)sumf[i+2][j] % mod) % mod;
		}
	}
	ans_c = (ans_c % mod + mod) % mod;
	ans_f = (ans_f % mod + mod) % mod;
	ans_c = (ans_c * (ll)c_num) % mod;
	ans_f = (ans_f * (ll)f_num) % mod;
	printf("%lld %lld\n", ans_c, ans_f);
}
int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int t, id; t = read(); id = read();
	while (t--) solve();
	return 0;
}