#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 1005;
const int P = 998244353;
const int inv2 = 499122177;
typedef long long ll;
int T, id;
int n, m, c, f, a[MAXN][MAXN];
int tr[MAXN][MAXN], td[MAXN][MAXN], lb[MAXN][MAXN], sq[MAXN][MAXN], s[MAXN][MAXN], sn[MAXN][MAXN];
int vc, vf;
char ins[MAXN];

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	scanf("%d %d", &T, &id);
	while (T--) {
		vc = vf = 0;
		memset(tr, 0, sizeof(tr));
		memset(td, 0, sizeof(td));
		memset(lb, 0, sizeof(lb));
		memset(sq, 0, sizeof(sq));
		memset(s, 0, sizeof(s));
		memset(sn, 0, sizeof(sn));
		scanf("%d %d %d %d", &n, &m, &c, &f);
		for (int i = 1; i <= n; i++) {
			scanf("%s", ins+1);
			for (int j = 1; j <= m; j++) {
				a[i][j] = (ins[j]=='1');
			}
		}
		for (int i = 1; i <= n; i++) {
			tr[i][m+1] = 0;
			for (int j = m; j >= 1; j--) {
				if (a[i][j]==1) {
					tr[i][j] = 0;
				}
				else tr[i][j] = tr[i][j+1]+1;
			}
		}
		for (int j = 1; j <= m; j++) {
			td[n+1][j] = 0;
			lb[n+1][j] = n+1;
			for (int i = n; i >= 1; i--) {
				if (a[i][j]==1) {
					td[i][j] = 0;
					lb[i][j] = i;
				}
				else {
					td[i][j] = td[i+1][j]+1, lb[i][j] = lb[i+1][j];
				}
			}
		}
		for (int j = 1; j <= m; j++) {
			for (int i = 1; i <= n; i++) {
				if (a[i][j] == 1) {
					s[i][j] = 0;
					sq[i][j] = 0;
					sn[i][j] = 0;
				}
				else {
					s[i][j]  = (s[i-1][j]+tr[i][j]-1)%P;
					sq[i][j] = (sq[i-1][j]+(ll)(tr[i][j]-1)*(tr[i][j]-1)%P)%P;
					if (tr[i-1][j] > 0)
						sn[i][j] = (sn[i-1][j]+(ll)(tr[i][j]-1)*(tr[i-1][j]-1)%P)%P;
					else
						sn[i][j] = 0;
				}
			}
		}
		// solve vc
		for (int j = 1; j <= m; j++) {
			int top = 0;
			for (int i = 1; i <= n; i++) {
				if (a[i][j] == 1) {
					top = i;
					continue;
				}
				if (i - top <= 2) continue;
				vc = (vc + (ll)(tr[i][j]-1)*(s[i-2][j]-s[top][j])%P)%P;
			}
		}
		// solve vf
		for (int j = 1; j <= m; j++) {
			int top = 0;
			for (int i = 1; i <= n; i++) {
				if (a[i][j] == 1) {
					top = i;
					continue;
				}
				if (i - top <= 2) continue;
				int b = lb[i][j]-1;
				vf = (vf + ((ll)(td[i][j]-1)*(tr[i][j]-1)%P)*(ll)(s[i-2][j]-s[top][j])%P)%P;
			}
		}
		printf("%d %d\n", ((ll)c*vc%P+P)%P, ((ll)f*vf%P+P)%P);
	}
	return 0;
}