#include <bits/stdc++.h>
#define file(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
typedef long long ll;

const int N = 1e3 + 5;
const int M = 1e3 + 5;
const int mod = 998244353;

int gMr, id;
int n, m, c, f;
char map[N][M];

int r[N][M], d[N][M];

int main() {
	file("plant");
	scanf("%d%d", &gMr, &id);
	while (gMr --) {
		scanf("%d%d%d%d", &n, &m, &c, &f);
		for (int i = 1; i <= n; i ++)
			scanf("%s", map[i] + 1);
		for (int i = 1; i <= n; i ++) {
			r[i][n + 1] = 0;
			for (int j = n; j >= 1; j --) {
				if (map[i][j] == '0')
					r[i][j] = r[i][j + 1] + 1;
				else
					r[i][j] = 0;
			}
		}
		for (int i = 1; i <= n; i ++) {
			d[n + 1][i] = 0;
			for (int j = n; j >= 1; j --) {
				if (map[j][i] == '0')
					d[j][i] = d[j + 1][i] + 1;
				else
					d[j][i] = 0;
			}
		}
		ll ansc = 0, ansf = 0;
		for (int i = 1; i <= n - 2; i ++)
			for (int j = 1; j <= n; j ++) if (map[i][j] == '0' and d[i][j] >= 3) {
				for (int k = i + 2; k <= i + d[i][j] - 1; k ++) {
					ansc = (ansc + 1ll * (r[i][j] - 1) * (r[k][j] - 1)) % mod;
					ansf = (ansf + 1ll * (r[i][j] - 1) * (r[k][j] - 1) * (d[i][j] - (k - i + 1))) % mod;
				}
			}
		printf("%lld %lld\n", ansc * c % mod, ansf * f % mod);
	}
	return 0;
}