//sto myee orz
//9:47 solved
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define sfor(i, h, t) for(int i = (h); i <= (t); ++i)
#define dfor(i, t, h) for(int i = (t); i >= (h); --i)
#define il inline
#define ms(x, i) memset(x, i, sizeof x)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ldb;
const int MAXN = 1008;
const ll P = 0x3B800001;
int T, id;
int N, M;
ll wc, wf;
char mat[MAXN][MAXN];
int fa[MAXN][MAXN];
il int find(int x, int y) {
    return fa[x][y] == 0 ? 0 : fa[x][y] == y ? y : fa[x][y] = find(x, fa[x][y]);
}
il void merge(int x, int y1, int y2) { // (x, y1)->(x, y2)
    int f1 = find(x, y1), f2 = find(x, y2);
    if(f1 != f2) 
        fa[x][f1] = f2;
}
int fa_[MAXN][MAXN];
il int find_(int x, int y) {
    return fa_[x][y] == 0 ? 0 : fa_[x][y] == x ? x : fa_[x][y] = find_(fa_[x][y], y);
}
il void merge_(int x1, int x2, int y) { // (x1, y)->(x2, y)
    int f1 = find_(x1, y), f2 = find_(x2, y);
    if(f1 != f2)
        fa_[f1][y] = f2;
}
ll len[MAXN][MAXN], sum[MAXN][MAXN], sum_[MAXN][MAXN];
signed main(void) {
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> T >> id;
    while(T--) {
        ms(fa, 0);
        ms(fa_, 0);
        ms(len, 0);
        ms(sum, 0);
        ms(sum_, 0);
        cin >> N >> M >> wc >> wf;
        sfor(i, 1, N)
            sfor(j, 1, M) {
                cin >> mat[i][j];
                if(mat[i][j] == '0') {
                    fa[i][j] = j;
                    if(mat[i][j - 1] == '0')
                        merge(i, j - 1, j);
                    fa_[i][j] = i;
                    if(mat[i - 1][j] == '0')
                        merge_(i - 1, i, j);
                }
            }
        sfor(i, 1, N) 
            sfor(j, 1, M) {
                len[i][j] = find(i, j) - j;
                sum[i][j] = (len[i][j] < 0) ? 0 : ((sum[i - 1][j] + len[i][j]) % P);
                sum_[i][j] = (len[i][j] < 0) ? 0 : ((sum_[i - 1][j] + len[i][j] * (find_(i, j) - i)) % P);
            }
        // sfor(i, 1, N) {
            // sfor(j, 1, M)
                // cerr << sum_[i][j] << ' ';
            // cerr << '\n';
        // }
        ll ansc = 0, ansf = 0;
        sfor(y, 1, M) {
            int l = 1, r = 0;
            while(l <= N) {
                // // cerr << l << '\n';
                for(; mat[l][y] == '1'; ++l);
                if(l > N)
                    break;
                r = find_(l, y);
                sfor(x, l, r - 3) {
                    // yc1 = x + 2, yc2 = r
                    // yf1 = x + 2, yf2 = r - 1
                    // cerr << x << ' ' << y << ' ' << l << ' ' << r << ':';
                    (ansc += len[x][y] * (sum[r][y] - sum[x + 1][y] + P) % P) %= P;
                    (ansf += len[x][y] * (sum_[r][y] - sum_[x + 1][y] + P) % P) %= P;
                    // cerr << ansc << ' ' << ansf << '\n';
                }
                if(r - 2 >= l)
                    (ansc += len[r - 2][y] * len[r][y] % P) %= P;
                // // cerr << ansc << '\n';
                l = r + 1;
            }
        }
        cout << (ansc * wc % P) << ' ' << (ansf * wf % P) << '\n';
    }
    return !bool(cout << '\n');
}
/*
C-shape: 
*..(>=0)*   **
..(>=1)     *
*..(>=0)*   **

F-shape:
*..(>=0)*   **
..(>=1)     *
*..(>=0)*   **
..(>=0)     *
*/