#include<cstdio>
#include<iostream>
#include<cctype>
constexpr const int p = 998244353;
inline int maintain(int x) { return x >= p ? x - p : x; }
inline int plus(int x, int y) { return maintain(x + y); }
inline int minus(int x, int y) { return maintain(x - y + p); }
inline int mul(int x, int y) { return 1ll * x * y % p; }
int t, id, a[1003][1003], c, _f, n, m, f[1003][1003], g[1003][1003];
int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
	std::cin >> t >> id;
	while (t--) {
		std::cin >> n >> m >> c >> _f;
		for (int i = 1; i <= n; i++) {
			int c = std::cin.get();
			while (!isgraph(c)) c = std::cin.get();
			for (int j = 1; j <= m; j++) {
				a[i][j] = !(c - '0'), c = std::cin.get();
			}
		}
		for (int i = 1; i <= n; i++) {
			f[i][m + 1] = 0;
			for (int j = m; j >= 1; j--) {
				if (a[i][j] == 1) f[i][j] = f[i][j + 1] + 1;
				else f[i][j] = 0;
			}
		}
		for (int j = 1; j <= m; j++) g[n + 1][j] = 0;
		for (int i = n; i >= 1; i--) {
			for (int j = 1; j <= m; j++) {
				if (a[i][j] == 1) g[i][j] = g[i + 1][j] + 1;
			}
		}
		int C = 0, F = 0;
		for (int j = 1; j <= m; j++) {
			for (int i = n, t = 0; i >= 1; i--) {
				if (!a[i][j]) { t = 0; continue; }
				C = plus(C, mul(f[i][j] - 1, t));
				t = plus(t, f[i][j] - 1);
			}
			for (int i = n - 1; i >= 1; i--) {
				if (a[i][j] && a[i + 1][j]) C = minus(C, mul(f[i][j] - 1, f[i + 1][j] - 1));
			}
		}
		for (int j = 1; j <= m; j++) {
			for (int i = n, t = 0; i >= 1; i--) {
				if (!a[i][j]) { t = 0; continue; }
				F = plus(F, mul(f[i][j] - 1, t));
				t = plus(t, mul(g[i][j] - 1, f[i][j] - 1));
			}
			for (int i = n - 1; i >= 1; i--) {
				if (a[i][j] && a[i + 1][j]) {
					F = minus(F, mul(mul(f[i][j] - 1, f[i + 1][j] - 1), g[i + 1][j] - 1));
				}
			}
		}
		std::cout << c * C << ' ' << _f * F << '\n';
	}
}
