#include <bits/stdc++.h>
using namespace std;
const int N = 1005,P = 998244353;
using ll = long long;
char s[N][N];int n,m,c,f,SubtaskID,rlen[N][N],dlen[N][N];ll sum[N][N];
inline void add(ll &a,ll b) {a = (a + b) % P;}
int main ()
{
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
    ios::sync_with_stdio(false);
    int T;cin >> T >> SubtaskID;
    while (T--)
    {
        cin >> n >> m >> c >> f;
        for (int i = 1;i <= n + 1;i++)
            for (int j = 1;j <= m + 1;j++) rlen[i][j] = dlen[i][j] = 0;
        for (int i = 1;i <= n;i++) cin >> (s[i] + 1);
        for (int i = 1;i <= n;i++)
            for (int j = m;j > 0;--j)
                if (s[i][j] == '0') rlen[i][j] = rlen[i][j + 1] + 1;else rlen[i][j] = 0;
        for (int i = n;i > 0;--i)
            for (int j = 1;j <= m;j++) if (s[i][j] == '0') dlen[i][j] = dlen[i + 1][j] + 1;else dlen[i][j] = 0;
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++) sum[i][j] = 0;
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++) if (s[i][j] == '0') sum[i][j] = (sum[i - 1][j] + rlen[i][j] - 1 + P) % P;
        ll ansc = 0,ansf = 0;
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
                if (s[i][j] == '0' && s[i - 1][j] == '0')
                    add(ansc,sum[i - 2][j] * (rlen[i][j] - 1)),
                    add(ansf,sum[i - 2][j] * (rlen[i][j] - 1) % P * (dlen[i][j] - 1));
        cout << (c * ansc) << ' ' << (f * ansf) << '\n';
    }
    return 0;
}