#include <iostream>
#include <cstdio>

namespace SuperI{
	const int MBUF = 2 << 21;
	std::streambuf *inbuf = std::cin.rdbuf();
	char buf[MBUF], *head = buf, *tail = buf, ___c;
	inline char getchar() {
		if ((head == tail) && (tail = (head = buf) + inbuf->sgetn(buf, MBUF), head == tail)) return EOF;
		else return *head++;
	}
	inline void read(int *const p) {
		*p = 0;
		for (___c = getchar(); ___c < '0' || ___c > '9';)
		___c = getchar();
		for (; ___c >= '0' && ___c <= '9'; *p = *p * 10 + (___c ^ 48), ___c = getchar());
	}
	inline void read(long long *const p) {
		*p = 0;
		for (___c = getchar(); ___c < '0' || ___c > '9'; ___c = getchar());
		for (; ___c >= '0' && ___c <= '9'; *p = *p * 10 + (___c ^ 48), ___c = getchar());
	}
	inline void read(bool *const p) {
		for (___c = getchar(); ___c < '0' || ___c > '9'; ___c = getchar());
		*p = ___c ^ 48;
	}
}

const int MAXN = 1005;
const long long MOD = 998244353;
int T, id;
int n, m, c, f;
long long ansc, ansf;
bool a[MAXN][MAXN];
int lex[MAXN][MAXN], dex[MAXN][MAXN];

int main() {
	using SuperI::read;
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	
	read(&T);
	read(&id);
	for (int t = 1; t <= T; ++t) {
		read(&n);
		read(&m);
		read(&c);
		read(&f);
		for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) read(&a[i][j]);
		for (int i = 1; i <= n; ++i) {
			int tmp = 0;
			for (int j = m; j >= 1; --j) {
				if (a[i][j]) tmp = 0;
				else ++tmp;
				lex[i][j] = tmp;
			}
		}
		for (int j = 1; j <= n; ++j) {
			int tmp = 0;
			for (int i = n; i >= 1; --i) {
				if (a[i][j]) tmp = 0;
				else ++tmp;
				dex[i][j] = tmp;
			}
		}
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= m; ++j) {
				if (dex[i][j] < 3) continue;
				for (int k = 3; k <= dex[i][j]; ++k) {
					long long x = (lex[i][j] - 1) * (lex[i + k - 1][j] - 1);
					ansc = (ansc + x) % MOD;
					ansf = (ansf + x * (dex[i][j] - k)) % MOD;
				}
			}
		if (c) std::cout << ansc;
		else std::cout << 0;
		putchar(' ');
		if (f) std::cout << ansf;
		else std::cout << 0;
		putchar('\n');
	}
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}