#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#define up(i, a, b) for (int i = a; i <= b; i++)
#define dn(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long ll;
int read() {
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = getchar();}
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return x * f;
}
const int N = 1005, mod = 998244353;
int n, m, C, F, a[N][N], xia[N][N];
char mp[N][N];
int main() {
//	freopen("plant4.in", "r", stdin);
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T = read(), id = read();
	while (T--) {
		n = read(), m = read(), C = read(), F = read();
		up(i, 1, n) {
			scanf("%s", mp[i] + 1);
			up(j, 1, m) a[i][j] = (mp[i][j] - '0'), a[i][j] ^= 1, xia[i][j] = i;
		}
		dn(i, n, 1) {
			dn(j, m, 1) {
				if (a[i][j]) a[i][j] += a[i][j + 1];
				if (a[i][j] && a[i + 1][j]) xia[i][j] = xia[i + 1][j];
			}
		}
//		up(i, 1, n) {up(j, 1, m) printf("%d ", a[i][j]); puts("");}
//		up(i, 1, n) {up(j, 1, m) printf("%d ", xia[i][j]); puts("");}
		ll ans1 = 0, ans2 = 0;
		dn(j, m, 1) {
			ll res1 = 0, res2 = 0;
			dn(i, n, 1) {
				if (!a[i][j]) {
					res1 = res2 = 0;
					continue;
				}
				if (xia[i][j] >= i + 2 && a[i + 2][j] > 1) res1 += a[i + 2][j] - 1;
				if (xia[i][j] >= i + 2 && a[i + 2][j] > 1 && xia[i + 2][j] > i + 2) res2 += (ll)(a[i + 2][j] - 1) * (xia[i + 2][j] - (i + 2)) % mod;
				ans1 += (a[i][j] - 1) * res1 % mod, ans2 += (a[i][j] - 1) * res2 % mod;
				ans1 %= mod, ans2 %= mod;
			}
		}
		printf("%lld %lld\n", ans1 * C, ans2 * F);
	}
	return 0;
}













