#include <iostream>

using namespace std;
/*
尝试考虑一个简单的方法，就是与处理出来每个下表向下和向右分别延伸多少格子，然后进行具体的计算
*/
const int MAXN =1000 + 5;
const long long MOD = 998244353;
int a[MAXN][MAXN];
int lenR[MAXN][MAXN];
int lenD[MAXN][MAXN];
int main()
{
    freopen("plant.in", "r", stdin);
    freopen("plant.out","w",stdout);
    ios::sync_with_stdio(false);
    int T, id;
    cin >> T >> id;
    if (id == 1) {
        while (T--) {
            cout << 0 << ' ' << 0 << '\n';
        }
    } else {
        while (T--){
            int n, m, c, f;
            cin >> n >> m >> c >> f;
            for (int i = 1; i <= n; i++) {
                while (cin.peek() == '\n' || cin.peek() == '\r') {
                    cin.get();
                }
                for (int j = 1; j <= m; j++) {
                    char ch;
                    cin >> ch;
                    a[i][j] = int(ch == '1');
                }
            }
            for (int i = 1; i <= n; i++) {
                lenR[i][m] = !a[i][m];
                for (int j = m - 1; j >= 1; j--) {
                    if (!a[i][j]) {
                        lenR[i][j] = lenR[i][j + 1] + 1;
                    } else {
                        lenR[i][j] = 0;
                    }
                }
            }
            for (int j = 1; j <= m; j++){
                lenD[n][j] = !a[n][j];
                for (int i = n - 1; i >= 1; i--) {
                    if (!a[i][j]) {
                        lenD[i][j] = lenD[i + 1][j] + 1;
                    } else {
                        lenD[i][j] = 0;
                    }
                }
            }
            //接下来枚举每一列，扫描行，先80，柿子不会推了，我蒟蒻
            long long vc = 0ll, vp = 0ll;
            for (int j = 1; j <= m; j++) {
                for (int i1 = 1; i1 <= n; i1++) {
                    auto len1 = lenR[i1][j] - 1;
                    if (len1 >= 1 && lenD[i1][j] >= 3) {
                        for (int i2 = i1 + 2; i2 <= i1 + lenD[i1][j] - 1; i2++) {
                            auto len2 = lenR[i2][j] - 1;
                            if (len2 >= 1){
                                vc = (vc + len1 * len2) % MOD;
                                auto lend = lenD[i1][j] - (i2 - i1 + 1);
                                if (lend >= 1) {
                                    vp = (vp + len1 * len2 * lend) % MOD;
                                }
                            }
                        }
                    }
                }
            }
            cout << vc * c << ' ' << vp * f <<'\n';
        }
    }
    return 0;
}

