#include <iostream>
#include <cstdio>
#include <string>

constexpr int N = 1005;
constexpr int p = 998244353;

int add(int x, int y) { return (x += y) >= p ? x - p : x; }
int mul(int x, int y) { return static_cast<unsigned long long>(x) * y % p; }

int T, type;
int n, m, A, B;
std::string s;
int a[N][N];
int f[N][N], g[N][N];
int sum1[N][N], sum2[N][N];

int main() {
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    std::cin >> T >> type;
    while (T--) {
        std::cin >> n >> m >> A >> B;
        for (int i = 1; i <= n; ++i) {
            std::cin >> s;
            for (int j = 1; j <= m; ++j) a[i][j] = s[j - 1] == '0';
        }
        for (int i = 1; i <= n; ++i) {
            f[i][m + 1] = 0;
            for (int j = m; j; --j) f[i][j] = a[i][j] ? f[i][j + 1] + 1 : 0;
        }
        int ans1 = 0, ans2 = 0;
        for (int j = 1; j <= m; ++j) {
            g[n + 1][j] = 0;
            sum1[n + 1][j] = 0;
            sum2[n + 1][j] = 0;
            for (int i = n; i; --i) {
                g[i][j] = a[i][j] ? g[i + 1][j] + 1 : 0;
                sum1[i][j] = a[i][j] ? add(sum1[i + 1][j], f[i][j] - 1) : 0;
                sum2[i][j] = a[i][j] ? add(sum2[i + 1][j], mul(f[i][j] - 1, g[i][j] - 1)) : 0;
                if (i + 2 <= n && a[i][j] && a[i + 1][j]) {
                    ans1 = add(ans1, mul(f[i][j] - 1, sum1[i + 2][j]));
                    ans2 = add(ans2, mul(f[i][j] - 1, sum2[i + 2][j]));
                }
            }
        }
        std::cout << (A ? ans1 : 0) << ' ' << (B ? ans2 : 0) << '\n';
    }
    return 0;
}
