#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define Min(x, y) (x = min(x, y))
#define Max(x, y) (x = max(x, y))
#define fir(x, y, z) for(int x = (y); x <= (z); ++ x)
#define fdr(x, y, z) for(int x = (y); x >= (z); -- x)
#define fre(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

namespace io {
	const int n = 1e6;
	char c, b[n], *i, *j;
	inline char gc() {
		if (i == j) {
			j = (i = b) + fread(b, 1, n, stdin);
			if (i == j) return EOF;
		} return *i ++;
	}
	#define gc getchar
	template <typename T = int>
	inline T read() {
		T s = 0; int x = 0;
		while (!isdigit(c = gc())) x |= c == '-';
		for (; isdigit(c); c = gc())
			s = (s << 1) + (s << 3) + (c & 15);
		return x ? - s : s;
	}
}

using io :: read;

const int N = 1e3 + 5;
const int P = 998244353;

int n, m, c, f;
int down[N][N], r[N][N], up[N][N];
char s[N][N];

int main() {
	fre(plant);
	for (int T = read(), id = read(); T --;) {
		n = read(); m = read(); c = read(); f = read();
		fir (i, 1, n) scanf("%s", s[i] + 1);
		memset(down, 0, sizeof down);
		memset(r, 0, sizeof r);
		memset(up, 0, sizeof up);
		fir (i, 1, n) {
			fdr (j, m, 1) {
				if (s[i][j] == '1' || j == m || s[i][j + 1] == '1') {
					r[i][j] = 0; continue;
				}
				r[i][j] = r[i][j + 1] + 1;
			}
		}
		fir (i, 1, n) {
			fir (j, 1, m) {
				if (s[i][j] == '1') {
					up[i][j] = 0;
					continue;
				}
				up[i][j] = r[i][j] + up[i - 1][j];
			}
		}
		fir (j, 1, m) {
			fdr (i, n, 1) {
				if (s[i][j] == '1' || i == n || s[i + 1][j] == '1') {
					down[i][j] = 0; continue;
				}
				down[i][j] = down[i + 1][j] + 1;
			}
		}
		ll vc = 0, vf = 0;
		fir (i, 3, n) {
			fir (j, 1, m - 1) {
				if (s[i - 1][j] == '1') continue;
				vc += 1ll * up[i - 2][j] * r[i][j] % P;
				vf += 1ll * up[i - 2][j] * r[i][j] % P * down[i][j] % P;
			}
		}
		printf("%lld %lld\n", c * vc % P, f * vf % P);
	}
	return 0;
}