#include <bits/stdc++.h>
using namespace std;
const int N = 1005, mod = 998244353;
int n, m, C, F, a[N][N], rc[N][N], dc[N][N], sum[N][N], sum2[N][N];
int calc(int i, int l, int r) {
	if (l > r) return 0; 
	return (sum[r][i] - sum[l - 1][i] + mod) % mod;
}
int calc2(int i, int l, int r) {
	if (l > r) return 0; 
	return (sum2[r][i] - sum2[l - 1][i] + mod) % mod;
}
void func() {
	memset(sum, 0, sizeof(sum));
	memset(sum2, 0, sizeof(sum2));
	scanf("%d%d%d%d", &n, &m, &C, &F);
	int ansc = 0, ansf = 0;
	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++) {
		rc[i][m + 1] = 0;
		for (int j = m; j >= 1; j--)
			if (a[i][j] == 0) rc[i][j] = rc[i][j + 1] + 1;
			else rc[i][j] = 0;
	}
	for (int j = 1; j <= m; j++) {
		dc[n + 1][j] = 0;
		for (int i = n; i >= 1; i--)
			if (a[i][j] == 0) dc[i][j] = dc[i + 1][j] + 1;
			else dc[i][j] = 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			sum[i][j] = (sum[i - 1][j] + rc[i][j]) % mod;
			sum2[i][j] = (sum2[i - 1][j] + 1ll * max(dc[i][j] - 1, 0) *
				max(rc[i][j] - 1, 0)) % mod;
		}
			
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) if (a[i][j] == 0) {
			int k = i + dc[i][j] - 1;
			if (i + 2 <= k) {
				ansc = (ansc + 1ll * (rc[i][j] - 1) * (calc(j, i + 2, k)
					- (k - i - 1) + mod)) % mod;
				ansf = (ansf + 1ll * (rc[i][j] - 1) * calc2(j, i + 2, k)) % mod;
			}
		}
	}
	printf("%d %d\n", ansc * C, ansf * F);
}
		
int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T, id;
	scanf("%d%d", &T, &id);
	while (T--) func();
	return 0;
}