// 简单记录下向右向上向下拓展的个数，还是算简单题，普及-？
# include <bits/stdc++.h>
# define int long long
# define wheneveright bool mem2; signed main
using namespace std;

bool mem1;

const int maxn = 1005;
const int mod = 998244353;

struct reader {
	reader & operator >> (char & ret) {
		for (ret = getchar (); !isdigit (ret); ret = getchar ()) ;
		return * this;
	}
	template <typename Type>
	reader & operator >> (Type & ret) {
		int f = 1; ret = 0; char ch = getchar ();
		for (;!isdigit (ch); ch = getchar ()) if (ch == '-') f = -f;
		for (; isdigit (ch); ch = getchar ()) ret = ret * 10 + ch - '0';
		ret *= f; return * this;
	}
} fin;

int T, n, m, c, f, res;
char a[maxn][maxn];
int R[maxn][maxn], U[maxn][maxn], D[maxn][maxn];

wheneveright () {
	cerr << "Memory Cost : " << abs (& mem1 - & mem2) / 1024. / 1024. << " MB\n";
	freopen ("plant.in", "r", stdin);
	freopen ("plant.out", "w", stdout);
	fin >> T >> res;
	while (T--) {
		fin >> n >> m >> c >> f; res = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) fin >> a[i][j];
		for (int i = 0; i <= n + 1; i++)
			for (int j = 0; j <= m + 1; j++) R[i][j] = U[i][j] = 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 <= m; i++) {
			for (int j = 1; j <= n; j++) {
				if (a[j][i] == '1') U[j][i] = 0;
				else U[j][i] = (U[j - 1][i] + R[j][i] - 1) % mod;
			}
			for (int j = n; j >= 1; j--) {
				if (a[j][i] == '1') D[j][i] = 0;
				else D[j][i] = D[j + 1][i] + 1;
			}
		}
		// C
		for (int i = 3; i <= n; i++)
			for (int j = 1; j <= m; j++)
				if (a[i][j] == '0' && a[i - 1][j] == '0' && a[i - 2][j] == '0')
					res = (res + (R[i][j] - 1) * U[i - 2][j] % mod) % mod;
		printf ("%lld ", res * c % mod); res = 0;
		// F
		for (int i = 3; i < n; i++)
			for (int j = 1; j <= m; j++)
				if (a[i][j] == '0' && a[i - 1][j] == '0' && a[i - 2][j] == '0' && a[i + 1][j] == '0')
					res = (res + (R[i][j] - 1) * U[i - 2][j] % mod * D[i + 1][j] % mod) % mod;
		printf ("%lld\n", res * f % mod);
	}
	return 0;
}