// Warning : NEED Confirm!

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <vector>

typedef long long LL;
#define rep(i,a,b) for(int i=a,_ds##i=b;i<=_ds##i;++i)
#define per(i,a,b) for(int i=a,_ds##i=b;i>=_ds##i;--i)
template<typename T> T rd(){T r;char c,n=0;while(!isdigit(c=getchar()))n=c=='-';r=c-'0';while(isdigit(c=getchar()))r=r*10+c-'0';return n?-r:r;}
#define rdi rd<int>
#define rdl rd<LL>

const int N = 1005;
const LL P = 998244353;

int T, id;
int n, m, c, f;
char s[N][N];
LL suf[N][N];

int main() {
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    T = rdi(), id = rdi();
    while (T--) {
        n = rdi(), m = rdi(), c = rdi(), f = rdi();
        rep(i, 1, n) {
            scanf("%s", s[i] + 1);
            suf[i][n + 1] = -1;
            per(j, n, 1) {
                s[i][j] -= '0';
                if (s[i][j]) suf[i][j] = -1;
                else suf[i][j] = suf[i][j+1] + 1;
            }
        }
        LL ansC = 0, ansF = 0;
        rep(j, 1, m) {
            LL s1 = 0, s2 = 0, t = 0;
            int cnt = 0;
            rep(i, 1, n) {
                if (s[i][j]) {
                    s1 = s2 = t = cnt = 0;
                } else {
                    ++cnt;
                    if (cnt >= 4) {
                        (s2 += suf[i-3][j]) %= P;
                        (t += suf[i-1][j] * s2) %= P;
                        (ansF += t) %= P;
                    }
                    if (cnt >= 3) {
                        (s1 += suf[i-2][j]) %= P;
                        (ansC += suf[i][j] * s1) %= P;
                    }
                }
            }
        }
        printf("%lld %lld\n", ansC * c % P, ansF * f % P);
    }
    return 0;
}
