#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -f; c = getchar();}
	while (c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}
	return x * f;
}
const int N = 1050, mod = 998244353;
int T, id;	
int n, m, C, F;
char a[N][N];
int g[N][N], R[N][N], D[N][N];
inline void add(int &x, int y) {
	x += y; if (x >= mod) x %= mod;
}
inline void solveC() {
	memset(g, 0, sizeof g);
	memset(R, 0, sizeof R); int ans = 0;
	for (int i = 1; i <= n; ++ i)
		for (int j = m; j >= 1; -- j)
			if (a[i][j] == '1') R[i][j] = 0;
			else R[i][j] = R[i][j+1] + 1;
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= m; ++ j) {
			if (a[i][j] == '1') continue;
			g[i][j] = g[i-1][j];
			if (i > 2 && R[i-2][j] > 1 && a[i-1][j] == '0' && a[i-2][j] == '0') add(g[i][j], R[i-2][j]-1);
		}
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= m; ++ j) {
			if (a[i][j] == '1') continue;
			add(ans, g[i][j] * (R[i][j]-1) % mod);
		}
	printf("%lld ", ans * C);
}
inline void solveF() {
	int ans = 0; memset(D, 0, sizeof D);
	for (int i = n; i >= 1; -- i) 
		for (int j = 1; j <= m; ++ j)
			if (a[i][j] == '1') D[i][j] = 0;
			else D[i][j] = D[i+1][j] + 1;
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= m; ++ j) {
			if (a[i][j] == '1') continue;
			add(ans, g[i][j] * (R[i][j]-1) % mod * (D[i][j]-1) % mod);
		}
	printf("%lld", ans * F);
}
signed main () {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	T = read(), id = read();
	while (T --) {
		n = read(), m = read(), C = read(), F = read();
		for (int i = 1; i <= n; ++ i)
			for (int j = 1; j <= m; ++ j)
				scanf(" %c", &a[i][j]);
		solveC();
		solveF();
		puts("");
	}
	return 0;
}
