#include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define reset(x, v) memset(x, v, sizeof x)
#define int long long
using namespace std;

using ll = long long;

const int N = 1005, P = 998244353;

int n, m, c, f, r[N][N], d[N][N], F[N][N], sum[N][N];
char a[N][N];

void solve() {
	cin >> n >> m >> c >> f;
	rep(i, 1, n) rep(j, 1, m) cin >> a[i][j];
	rep(i, 1, n) per(j, m, 1) {
		if (a[i][j] != '1') r[i][j] = r[i][j + 1] + 1;
	}
//	rep(i, 1, n) {
//		rep(j, 1, m) cerr << r[i][j] << ' ';
//		cerr << '\n';
//	}
	int ans = 0;
	per(i, n, 1) rep(j, 1, m) {
		if (a[i][j] == '1') continue;
		d[i][j] = d[i + 1][j] + 1;
		(sum[i][j] += sum[i + 1][j] + r[i][j] - 1) %= P;
		int tt = sum[i + 1][j];
		if (a[i + 1][j] != '1') tt = tt - r[i + 1][j] + 1;
		if (i <= n - 2) ans = (ans + (ll) (r[i][j] - 1) * tt % P) % P;
//		cerr << "#" << sum[i][j] << ' ' << r[i][j] << "#" << '\n';
		F[i][j] = (ll) d[i + 1][j] * r[i][j + 1] % P;
	}
//	rep(i, 1, n) {
//		rep(j, 1, m) cerr << F[i][j] << ' ';
//		cerr << '\n';
//	}
	cout << (c * ans + P) % P << ' ';
	ans = 0;
	reset(sum, 0);
	per(i, n, 1) rep(j, 1, m) {
		if (a[i][j] == '1') continue;
		(sum[i][j] += sum[i + 1][j] + F[i][j]) %= P;
		if (i <= n - 3) ans = (ans + (ll) (r[i][j] - 1) * (sum[i + 1][j] - F[i + 1][j] % P) % P) % P;
	}
//	rep(i, 1, n) {
//		rep(j, 1, m) cerr << sum[i][j] << ' ';
//		cerr << '\n';
//	}
	cout << (f * ans + P) % P << '\n';
}

signed main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	cin.tie(nullptr)->sync_with_stdio(false);
	int T, id;
	cin >> T >> id;
	while (T--) {
		reset(r, 0);
		reset(d, 0);
		reset(F, 0);
		reset(sum, 0);
		solve();
	}
	return 0;
}
