//biu#9961miss
#include<bits/stdc++.h>
#define int long long
const int MOD = 998244353;
const int N = 1005;
int n, m, c, f;
char a[N][N];
int hr[N][N], hd[N][N];
int vc, vf;
void slv(){
    scanf("%lld%lld%lld%lld", &n, &m, &c, &f);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            scanf(" %c", &a[i][j]),
            a[i][j] -= '0';
    memset(hr, 0, sizeof(hr));
    memset(hd, 0, sizeof(hd));
    vc = vf = 0;
    for(int i=1; i<=n; i++)
        for(int j=m; j; j--)
            hr[i][j] = a[i][j]? 0 : hr[i][j+1]+1;
    for(int i=n; i; i--)
        for(int j=1; j<=m; j++)
            hd[i][j] = a[i][j]? 0 : hd[i+1][j]+1;
    for(int i=n; i; i--)
        for(int j=1; j<=m; j++)
            hd[i][j] = ((hr[i][j]-1)*(hd[i][j]-1) +
                (a[i+1][j]? 0 : hd[i+1][j])) % MOD;
    for(int i=n; i; i--)
        for(int j=1; j<=m; j++)
            hr[i][j] = (hr[i][j]-1 +
                (a[i+1][j]? 0 : hr[i+1][j])) % MOD;
    for(int j=1; j<=m; j++)
        for(int i=1; i<=n; i++)
            if(a[i][j]==0 && a[i+1][j]==0 && a[i+2][j]==0){
                vc += (hr[i][j]-hr[i+1][j]) * hr[i+2][j] % MOD;
                vf += (hr[i][j]-hr[i+1][j]) * hd[i+2][j] % MOD;
                vc %= MOD, vf %= MOD;
            }
    printf("%lld %lld\n", (vc*c)%MOD, (vf*f)%MOD);
}
int T, id;
signed main(){
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    scanf("%lld%lld", &T, &id);
    while(T--) slv();
    return 0;
}