#include<bits/stdc++.h>
#define ll long long
#define rep(i, l, r) for (int i=l, _=r; i<=_; i++)
#define per(i, l, r) for (int i=l, _=r; i>=_; i--)
using namespace std;

ll read() {
    ll s=0, t=0; char ch=getchar();
    for (; ch<'0'||ch>'9'; ch=getchar()) if (ch=='-') t=1;
    for (; ch>='0'&&ch<='9'; ch=getchar()) s=s*10+ch-'0';
    return t?-s:s;
}
const int M=1005, ljc=998244353;
int a[M][M], ri[M][M], dw[M][M], f[M][M];
int main() {
    freopen("plant.in", "r", stdin);
    freopen("plant.out", "w", stdout);
    for (int T=read(), id=read(); T; T--) {
        int n=read(), m=read(), C=read(), F=read();
        memset(ri, 0, sizeof(ri));
        memset(dw, 0, sizeof(dw));
        memset(f, 0, sizeof(f));
        memset(a, 0, sizeof(a));
        rep(i, 1, n) rep(j, 1, m) {
            int x=getchar();
            while (x<'0'||x>'1') x=getchar();
            a[i][j]=x-'0';
        }
        rep(i, 1, n) rep(j, 1, m) printf("%d%c", a[i][j], " \n"[j==m]);
        per(i, n, 1) per(j, m, 1) {
            ri[i][j]=a[i][j]?0:ri[i][j+1]+1;
            dw[i][j]=a[i][j]?0:dw[i+1][j]+1;
        }
        ll ans1=0, ans2=0;
        rep(i, 1, n) rep(j, 1, m) if (!a[i][j]){
            int tmp=(ll)f[i-1][j]*(ri[i][j]-1)%ljc;
            ans1=(ans1+tmp)%ljc;
            ans2=(ans2+(ll)tmp*(dw[i][j]-1))%ljc;
            f[i][j]=f[i-1][j]+(a[i-1][j]?0:ri[i-1][j+1]);
        }
        printf("%lld %lld\n", C?ans1:0ll, F?ans2:0ll);
    }
}
//this example 3 is so smelly.