#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, c, f;
char s[1010][1010];
int a[1010][1010];
void rmain() {
    scanf("%d%d%d%d", &n, &m, &c, &f);
    for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
    long long ansc = 0, ansf = 0;
    for (int i = 1; i <= n; i++) {
        a[i][m + 1] = -1;
        for (int j = m; j > 0; j--) a[i][j] = s[i][j] == '0' ? a[i][j + 1] + 1 : -1;
    }
    for (int j = 1; j <= m; j++) {
        for (int l = 1, r; l <= n; l = r + 1) {
            if (a[l][j] == -1) {
                r = l;
                continue;
            }
            for (r = l; r < n && a[r + 1][j] != -1; r++);
            int rest = r - l + 1, sum = 0;
            for (int i = l; i <= r; i++) {
                rest--;
                if (i - 2 >= l) sum += a[i - 2][j];
                ansc += 1ll * sum * a[i][j];
                ansf += 1ll * sum * a[i][j] * rest;
            }
        }
    }
    ansc %= mod, ansf %= mod;
    printf("%lld %lld\n", ansc * c, ansf * f);
}
int main() {
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    int T;
    scanf("%d%*d", &T);
    while (T--) rmain();
}