#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1005, mod = 998244353;
int T, id;
int n, m, c, f;
char s[N][N];
int mxR[N][N], sumD[N][N];
int SumD[N][N];
int ansC, ansF;

void add(int &a, int b) {
	a += b;
	if (a >= mod) a -= mod;
}

void solveC() {
	if (!c) return printf("%d ", 0), void();
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m + 1; ++j) if (s[i][j] == '1') {
			for (int k = j - 1; k; --k) {
				if (s[i][k] == '1') break;
				mxR[i][k] = j - k - 1;
			}
		}
	for (int i = 1; i <= n + 1; ++i)
		for (int j = 1; j <= m; ++j) if (s[i][j] == '1') {
			int sum = 0;
			for (int k = i - 1; k; --k) {
				if (s[k - 1][j] == '1') break;
				if (k - 2 <= 0) break;
				sum += mxR[k][j];
				if (s[k - 2][j] == '1') break;
				sumD[k - 2][j] = sum;
			}
		}
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) if (s[i][j] == '0')
			add(ansC, 1ll * mxR[i][j] * sumD[i][j] % mod);
	printf("%d ", ansC);
			
}

void solveF() {
	if (!f) return printf("%d\n", 0), void();
	for (int i = 1; i <= n + 1; ++i)
		for (int j = 1; j <= m; ++j) if (s[i][j] == '1' && i > 1 && s[i - 1][j] == '0') {
			int sum = 0;
			for (int k = i - 1; k; --k) {
				if (s[k - 1][j] == '1') break;
				if (k - 2 <= 0) break;
				add(sum, 1ll * mxR[k][j] * (i - k - 1) % mod);
				if (s[k - 2][j] == '1') break;
				SumD[k - 2][j] = sum;
			}
		}
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) if (s[i][j] = '0')
			add(ansF, 1ll * mxR[i][j] * SumD[i][j] % mod);
	printf("%d\n", ansF);
}

void solve() {
	scanf("%d%d%d%d", &n, &m, &c, &f);
	for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
	for (int i = 1; i <= n; ++i) s[i][0] = s[i][m + 1] = '1';
	for (int j = 0; j <= m + 1; ++j) s[n + 1][j] = '1';
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) mxR[i][j] = sumD[i][j] = SumD[i][j] = 0;
	ansC = ansF = 0;
	solveC();
	solveF();
}

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