#include <bits/stdc++.h>

using namespace std;

namespace sub1 {
const int N = 1002;
const int P =	998244353;
typedef long long LL;
int T, id, n, m, c, f, a[N][N], r[N][N], u[N][N], d[N][N], VC, VF;

void main() {
	scanf("%d%d", &T, &id);
	while (T--) {
		scanf("%d%d%d%d", &n, &m, &c, &f);
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= m; ++j)
				scanf("%1d", &a[i][j]);
		memset(r, 0, sizeof(r));
		memset(u, 0, sizeof(u));
		memset(d, 0, sizeof(d));
		VC = VF = 0;
		for (int i = 1; i <= n; ++i)
			for (int j = m; j; --j)
				if (!a[i][j]) {
					r[i][j] = r[i][j + 1] + 1;
					u[i][j] = u[i - 1][j] + r[i][j + 1];
				}
		for (int i = n; i > 1; --i)
			for (int j = 1; j <= m; ++j)
				if (!a[i][j]) {
					d[i][j] = d[i + 1][j] + 1;
					if (!a[i - 1][j]) {
						VC = (VC + (LL)r[i][j + 1] * u[i - 2][j]) % P;
						VF = (VF + (LL)r[i][j + 1] * u[i - 2][j] * d[i + 1][j]) % P;
					}
				}
		printf("%d %d\n", c * VC, f * VF);
	}
}
}

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	sub1::main();
	fclose(stdin);
	fclose(stdout);
	return 0;
}
