#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::cerr;

#define int long long
constexpr int N = 1005, P = 998244353;
int n, m, c, f, ans1, ans2;
int a[N][N], suml[N][N], sumc[N][N];
int recl[N][N], recc[N][N];

signed main() {
    freopen("plant.in", "r", stdin), freopen("plant.out", "w", stdout);
    std::ios::sync_with_stdio(false), cin.tie(nullptr);
    int T, id;
    cin >> T >> id;
    while (T--) {
        ans1 = ans2 = 0;
        cin >> n >> m >> c >> f;
        if (c == 0 && f == 0) {
            cout << "0 0\n";
            continue;
        }
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j) {
                char ch;
                cin >> ch;
                a[i][j] = ch - '0';
            }
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                suml[i][j] = a[i][j] + suml[i][j - 1];
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                sumc[i][j] = a[i][j] + sumc[i - 1][j];
        for (int i = 1; i <= n; ++i) {
            int t = m + 1;
            recl[i][m] = t;
            for (int j = m - 1; j; --j) {
                if (suml[i][j + 1] != suml[i][j]) t = j + 1;
                recl[i][j] = t;
            }
        }
        for (int j = 1; j <= m; ++j) {
            int t = n + 1;
            recc[n][j] = t;
            for (int i = n - 1; i; --i) {
                if (sumc[i + 1][j] != sumc[i][j]) t = i + 1;
                recc[i][j] = t;
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = i + 2; j <= n; ++j) {
                for (int c = 1; c <= m; ++c) {
                    if (a[i][c] == 1 || a[j][c] == 1 || sumc[i][c] != sumc[j][c]) continue;
                    int k1 = recl[i][c], k2 = recl[j][c];
                    // while (suml[i][k1] == suml[i][c] && k1 <= m) ++k1;
                    // while (suml[j][k2] == suml[j][c] && k2 <= m) ++k2;
                    (ans1 += (k1 - c - 1) * (k2 - c - 1) % P) %= P;
                    int k3 = recc[j][c];
                    // while (sumc[k3][c] == sumc[j][c] && k3 <= n) ++k3;
                    (ans2 += (k1 - c - 1) * (k2 - c - 1) % P * (k3 - j - 1) % P) %= P;
                }
            }
        }
        cout << c * ans1 % P << ' ' << f * ans2 % P << '\n';
    }


    return 0;
}
