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

using ll = long long;
const ll p = 998244353;

inline void Inc(ll & x, ll y) {
	x = (x + y + p) % p;
}

const int N = 1005;
char a[N][N];
ll S[N][N], F[N][N], G[N][N];

void Solve() {
	int n, m;
	ll c, f;
	scanf("%d%d%lld%lld", &n, &m, &c, &f);
	for (int i = 1; i <= n; ++i) scanf("%s", a[i] + 1);
	for (int i = 1; i <= n; ++i) {
		S[i][m + 1] = 0;
		for (int j = m; j > 0; --j) 
			S[i][j] = (a[i][j] == '0' ? S[i][j + 1] + 1 : 0);
		
	}
	for (int i = 1; i <= m; ++i) F[0][i] = 0;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			F[i][j] = (F[i - 1][j] + S[i][j] + p - 1) % p;
	for (int i = 1; i <= m; ++i) G[0][i] = 0;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			G[i][j] = (G[i - 1][j] + F[i][j]) % p;
	
	ll ansc = 0, ansf = 0;
	for (int i = 1; i <= m; ++i) {
		for (int l = 1, r; l <= n; l = r + 1) {
			r = l;
			if (!S[l][i]) continue;
			while (r < n && S[r + 1][i]) ++r;
			for (int A = l; A + 3 <= r; ++A)
				Inc(ansf, (S[A][i] - 1) * (G[r - 1][i] - G[A + 1][i] - F[A + 1][i] * (r - A - 2) % p) % p);
		}
	}
	
	for (int i = 1; i <= m; ++i) {
		for (int l = 1, r; l <= n; l = r + 1) {
			r = l;
			if (!S[l][i]) continue;
			while (r < n && S[r + 1][i]) ++r;
			for (int A = l; A + 2 <= r; ++A) 
				Inc(ansc, (S[A][i] - 1) * (F[r][i] - F[A + 1][i]) % p);
		}
	}
	
	printf("%lld %lld\n", ansc * c % p, ansf * f % p);
}

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T, id;
	scanf("%d%d", &T, &id);
	while (T--) Solve();
	return 0;
}