#include <bits/stdc++.h>

constexpr int p = 998244353, N = 1005;

int f[N][N], g[N][N], a[N][N];
int d[N][N], r[N][N], pre[N];
std::string s;

int reduce (int x, int y) {
	if (x+y >= p) return x+y-p;
	return x+y;
}

int main () {
	std::ios::sync_with_stdio (false);
	std::cin.tie (nullptr), std::cout.tie (nullptr);
	freopen ("plant.in", "r", stdin);
	freopen ("plant.out", "w", stdout);
	int T, id;
	std::cin >> T >> id;
	while (T--) {
		int n, m, oc, of;
		std::cin >> n >> m >> oc >> of;
		long long vc = 0, vf = 0;
		for (int i = 1; i <= n; i++) {
			std::cin >> s;
			for (int j = 1; j <= m; j++) {
			 	f[i][j] = g[i][j] = 0;
			 	a[i][j] = (s[j-1] == '1');
			}
		}
		for (int j = 1; j <= m; j++) {
			d[n+1][j] = -1;
			for (int i = n; i >= 1; i--) {
				if (a[i][j]) d[i][j] = -1;
				else d[i][j] = std::max (i, d[i+1][j]);
			}
		}
		for (int i = 1; i <= n; i++) {
			d[i][m+1] = -1;
			for (int j = m; j >= 1; j--) {
				if (a[i][j]) r[i][j] = -1;
				else r[i][j] = std::max (j, r[i][j+1]);
			}
		}
//		for (int i = 1; i <= n; i++, std::cerr << '\n') for (int j = 1; j <= m; j++) std::cerr << d[i][j] << ' ';
//		for (int i = 1; i <= n; i++, std::cerr << '\n') for (int j = 1; j <= m; j++) std::cerr << r[i][j] << ' ';
		for (int j = 1; j <= m; j++) {
			int pos = 0;
			pre[0] = 0;
			for (int i = 1; i <= n; i++) {
				if (r[i][j] != -1) pre[i] = reduce (pre[i-1], r[i][j] - j);
				if (a[i][j]) pos = i;
				if (pos < i-2) {
					if (r[i][j] > j) {
						vc = reduce (vc, 1ll * (r[i][j] - j) * reduce (pre[i-2] - pre[pos], p) % p);
						if (d[i][j] > i)
							vf = reduce (vf, 1ll * (r[i][j] - j) * reduce (pre[i-2] - pre[pos], p) % p * (d[i][j] - i) % p); 
//						std::cout << vc << ' ' << vf << '\n';
					}
				}
			}
		}
		std::cout << oc * vc % p << ' ' << of * vf % p << '\n';
	}
	return 0;
}