#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mkp make_pair
#define fi first
#define se second
#define ls(x) ((x)<<1)
#define rs(x) ((x)<<1|1)
const int mod = 998244353, N = 1010;
int n, m, c, f, nxt[N][N], dn[N][N], up[N][N], sum[N][N];
char mp[N][N];
inline int calc(int j, int l, int r) {
	r = min(r, n); l = max(l, 1);
	if(l > r) return 0;
	int ret = sum[r][j] + mod - sum[l - 1][j];
	if(ret >= mod) ret -= mod;
	return ret;
}
void init() {
	memset(nxt, 0, sizeof(nxt));
	memset(dn, 0, sizeof(dn));
	memset(up, 0, sizeof(up));
	memset(sum, 0, sizeof(sum));
}
int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T, id; scanf("%d%d", &T, &id);
	while(T--) {
		init();
		scanf("%d%d%d%d", &n, &m, &c, &f);
		for(int i = 1; i <= n; i++)
			scanf("%s", mp[i] + 1);
		for(int i = 1; i <= n; i++) {
			int lst = m + 1;
			for(int j = m; j >= 1; j--) {
				if(mp[i][j] == '1') lst = j;
				else nxt[i][j] = lst - j - 1; 				
			}
		}
		for(int j = 1; j <= m; j++) {
			int lst = n + 1;
			for(int i = n; i >= 1; i--) {
				if(mp[i][j] == '1') lst = i;
				else dn[i][j] = lst - i - 1;
			}
			lst = 0;
			for(int i = 1; i <= n; i++) {
				if(mp[i][j] == '1') lst = i;
				else up[i][j] = i - lst - 1;
			}
		}
		for(int j = 1; j <= m; j++) {
			for(int i = 1; i <= n; i++) {
				sum[i][j] = sum[i - 1][j] + nxt[i][j];
				if(sum[i][j] >= mod) sum[i][j] -= mod;
			}
		}
		int ansc = 0, ansf = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++)
				if(mp[i][j] == '0') {
					ansc += (ll)nxt[i][j] * calc(j, i - up[i][j], i - 2) % mod;
					if(ansc >= mod) ansc -= mod;
					
					ansf += (ll)nxt[i][j] * calc(j, i - up[i][j], i - 2) % mod * dn[i][j] % mod;
					if(ansf >= mod) ansf -= mod;
				}
		}
		if(!c) ansc = 0; if(!f) ansf = 0;
		printf("%d %d\n", ansc, ansf);
	}
	return 0;
}
/*
?现在的T1真先进。
希望9:10能完事。
*/