#include<bits/stdc++.h>
using namespace std;

#define int long long
#define siz(a) ((int) (a.size()))
#define vi vector<int>
#define ll long long
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

const int N = 1000, mod = 998244353;
int T, id, a, b, c, d, f[N + 5][N + 5], p[N + 5][N + 5], g[N + 5][N + 5];
char s[N + 5][N + 5];

void c1() {
	memset(f, 0, sizeof(f));
	memset(g, 0, sizeof(g));
	int ans = 0;
	rep(i, 1, a) rep(j, 1, b) {
		if(s[i][j] == '1') continue;
		if(s[i - 1][j] == '0') f[i][j] = f[i - 1][j] + p[i - 1][j] - 1;
		else f[i][j] = 0;
		(ans += f[i - 1][j] * (p[i][j] - 1)) %= mod;
	}
	cout << ans * c % mod << ' ';
}
void c2() {
	memset(f, 0, sizeof(f));
	memset(g, 0, sizeof(g));
	int ans = 0;
	rep(i, 1, a) rep(j, 1, b) {
		if(s[i][j] == '1') continue;
		if(s[i - 1][j] == '0') {
			f[i][j] = f[i - 1][j] + p[i - 1][j] - 1;
			g[i][j] = (g[i - 1][j] + f[i - 1][j] * (p[i][j] - 1)) % mod;
		}
		else f[i][j] = g[i][j] = 0;
		(ans += g[i - 1][j]) %= mod;
	}
	cout << ans * d % mod << '\n';
}

signed main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	for(cin >> T >> id; T--; ) {
		cin >> a >> b >> c >> d;
		rep(i, 1, a) cin >> (s[i] + 1);
		rep(i, 1, a) per(j, b, 1) p[i][j] = s[i][j] == '1' ? 0 : p[i][j + 1] + 1;
		c1();
		c2();
	}
	return 0;
}