#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

inline int read() {
    char c; bool f = true;
    while (!isdigit(c = getchar())) f = c != '-';
    int x = c ^ 48;
    while (isdigit(c = getchar())) x = x * 10 + (c ^ 48);
    return f ? x : -x;
}

const int N = 1010;
const int p = 998244353;

int T, id;
int n, m, c, f;
char s[N][N];
int l[N][N];
int sumc[N][N];
int sumf[N][N];

ll ans;

inline void solveC() {
    ans = 0;
    for (int i = 1; i <= n - 2; i++) {
        for (int j = 1; j <= m - 1; j++) {
            if (l[i][j] == 0) continue;
            if (s[i + 1][j] == '1') continue;
            ans += 1ll * (l[i][j] - 1) * (sumc[i + 2][j]) % p;
            if (ans >= p) ans -= p;
        }
    }
    // printf("%lld ", ans);
}

inline void solveF() {
    ans = 0;
    for (int i = 1; i <= n - 2; i++) {
        for (int j = 1; j <= m - 1; j++) {
            if (l[i][j] == 0) continue;
            if (s[i + 1][j] == '1') continue;
            ans += 1ll * (l[i][j] - 1) * (sumf[i + 2][j]) % p;
            // cout << "? " << i << " " << j << " " << l[i][j] - 1 << " * " << sumf[i + 2][j] << endl;
            if (ans >= p) ans -= p;
        }
    }
}

int main() {
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    T = read(); id = read();
    while (T--) {
        n = read(); m = read(); c = read(); f = read();
        for (int i = 1; i <= n; i++) {
            scanf("%s", s[i] + 1);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = m; j >= 1; j--) {
                if (s[i][j] == '1') {
                    l[i][j] = 0;
                } else {
                    l[i][j] = l[i][j + 1] + 1;
                }
            }
        }
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= m; j++) {
                if (s[i][j] == '1') {
                    sumc[i][j] = 0;
                } else {
                    sumc[i][j] = sumc[i + 1][j] + l[i][j] - 1;
                }
            }
        }
        for (int j = 1; j <= m; j++) {
            int prev = n + 1;
            for (int i = n; i >= 1; i--) {
                if (s[i][j] == '1') {
                    sumf[i][j] = 0;
                    prev = i;
                } else {
                    sumf[i][j] = sumf[i + 1][j] + (l[i][j] - 1) * (prev - i - 1);
                }
            }
        }
        ans = 0;
        if (c) {
            solveC();
            printf("%lld ", ans);
        }
        if (f) {
            solveF();
            printf("%lld ", ans);
        }
        puts("");
    }
    return 0;
}
/*
1 1
4 4 1 1
0000
0111
0000
0111
*/