#include <bits/stdc++.h>

template<class T, class...Cond>
using require_ = std::enable_if_t<std::__and_<T, Cond...>::value, bool>;

template<class T, require_<std::is_signed<T>> = true>
void read(T & x) {
    x = 0;
    bool f = false;
    char c = getchar();
    while (!isdigit(c)) f |= (c == '-'), c = getchar();
    while (isdigit(c)) x = (x * 10) - (c ^ 48), c = getchar();
    x = (f ? x : -x);
}

template<class T, require_<std::is_unsigned<T>> = true>
void read(T & x) {
    x = 0;
    char c = getchar();
    while (!isdigit(c)) c = getchar();
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}

void read(std::string & s) {
    s = "";
    char c = getchar();
    while (!isgraph(c)) c = getchar();
    while (isgraph(c)) s += c, c = getchar();
}

template<class T, class...Args>
void read(T & x, Args&...args) {
    read(x);
    read(args...);
}

constexpr int N = 1e3 + 5;

constexpr int Mod = 998244353;

int test_case;

int n, m;
std::string s[N];
int len[N][N];

inline int Add(int a, int b) { a += b; while (a >= Mod) a -= Mod; while (a < 0) a += Mod; return a; }
template<class...Args>
inline int Add(int a, int b, Args...args) { return Add(a, Add(b, args...)); }
template<class...Args>
inline int & cAdd(int & a, int b, Args...args) { return a = Add(a, b, args...); }

inline int Mul(int a, int b) { return 1ll * a * b % Mod; }
template<class...Args>
inline int Mul(int a, int b, Args...args) { return Mul(a, Mul(b, args...)); }
template<class...Args>
inline int & cMul(int & a, int b, Args...args) { return a = Mul(a, b, args...); }

void solve() {
    int c, f;
    read(n, m, c, f);
    for (int i = 1; i <= n; ++ i) {
        read(s[i]);
        s[i] = std::string("1") + s[i] + "1";
    }

    if (c == 0 && f == 0) {
        puts("0 0");
        return;
    }

    for (int i = 0; i <= n + 1; ++ i) {
        for (int j = 0; j <= m + 1; ++ j) {
            len[i][j] = 0;
        }
    }

    for (int i = 1; i <= n; ++ i) {
        len[i][m] = 0;
        for (int j = m - 1; j >= 1; -- j) {
            if (s[i][j] == '0' && s[i][j + 1] == '0') {
                len[i][j] = len[i][j + 1] + 1;
            } else {
                len[i][j] = 0;
            }
        }
    }

    // for (int i = 1; i <= n; ++ i) {
    //     for (int j = 1; j <= m; ++ j) {
    //         printf("%d ", len[i][j]);
    //     }
    //     puts("");
    // }
    // puts("");

    int ans_c = 0;
    int ans_f = 0;

    if (n <= 500) {
        std::vector<std::pair<int, int>> tmp;
        for (int j = 1; j <= m; ++ j) {
            for (int i = 1; i <= n - 2;) {
                if (s[i][j] == '0' && s[i + 1][j] == '0' && len[i][j]) {
                    int k = i + 1;
                    int nxt = i + 1;
                    tmp.clear();
                    while (k <= n && s[k + 1][j] == '0') {
                        if (k != i + 1 && len[k][j]) {
                            tmp.emplace_back(k, len[i][j] * len[k][j]);
                            if (nxt == i + 1) {
                                nxt = k;
                            }
                        }
                        ++ k;
                        if (len[k][j]) {
                            cAdd(ans_c, Mul(len[i][j], len[k][j]));
                            if (nxt == i + 1) {
                                nxt = k;
                            }
                        }
                    }
                    for (const auto & it : tmp) {
                        cAdd(ans_f, Mul(Add(k, -it.first), it.second));
                    }
                    i = nxt;
                } else {
                    ++ i;
                }
            }
        }
        printf("%d %d\n", Mul(c, ans_c), Mul(f, ans_f));
        return;
    }

    std::vector<std::pair<int, int>> tmp;
    std::vector<int> sum;
    for (int j = 1; j <= m; ++ j) {
        for (int i = 1; i <= n - 2;) {
            if (s[i][j] == '0' && s[i + 1][j] == '0' && len[i][j]) {
                int k = i + 1;
                tmp.clear();
                tmp.emplace_back(i, len[i][j]);
                while (k <= n && s[k + 1][j] == '0') {
                    ++ k;
                    if (len[k][j]) {
                        tmp.emplace_back(k, len[k][j]);
                    }
                }
                if (k == i + 1) {
                    continue;
                }
                sum.clear();
                sum.resize(tmp.size() + 2);
                for (int p = (int)(tmp.size()) - 2; p >= 0; -- p) {
                    sum[p] = (sum[p + 1] + tmp[p + 1].second) % Mod;
                }
                for (int p = 0; p + 1 < tmp.size(); ++ p) {
                    cAdd(ans_c, Mul(tmp[p].second, Add(sum[p], -((tmp[p].first + 1 == tmp[p + 1].first) ? tmp[p + 1].second : 0))));
                }
                if (tmp.back().first == k) {
                    tmp.pop_back();
                }
                sum.clear();
                sum.resize(tmp.size() + 2);
                for (int p = (int)(tmp.size()) - 2; p >= 0; -- p) {
                    sum[p] = Add(sum[p + 1], Mul(tmp[p + 1].second, Add(k, -tmp[p + 1].first)));
                }
                for (int p = 0; p + 1 < tmp.size(); ++ p) {
                    cAdd(ans_f, Mul(tmp[p].second, Add(sum[p], -((tmp[p].first + 1 == tmp[p + 1].first) ? tmp[p + 1].second : 0))));
                }
                i = k + 1;
            } else {
                ++ i;
            }
        }
    }

    printf("%d %d\n", Mul(c, ans_c), Mul(f, ans_f));
}

int main() {
    std::freopen("plant.in", "r", stdin);
    std::freopen("plant.out", "w", stdout);

    int T = 1;
    read(T, test_case);
    for (int t = 1; t <= T; ++ t) {
        solve();
    }
    return 0;
}
/*
(1 0
4 3 1 1
001
010
000
000
)
4 2
*/
/*
(1 0
6 6 1 1
000010
011000
000110
010000
011000
000000
)
35 9
*/
