#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define per(i, x, y) for (int i = x; i >= y; i--)
using namespace std;

typedef long long ll;
const int mod = 998244353, N = 1e3 + 10;

void Add(int &x, int y) {
  x = (x + y) % mod;
}
void Mul(int &x, int y) {
  x = 1ll * x * y % mod;
}
void Sub(int &x, int y) {
  x = ((x - y) % mod + mod) % mod;
}
int mul(int x, int y) {
  return 1ll * x * y % mod;
}

int T, idx, n, m, _c, _f;
int ri[N][N], a[N][N], dw[N][N];
char s[N][N];

int main() {
  freopen("plant.in", "r", stdin);
  freopen("plant.out", "w", stdout);

  scanf("%d%d", &T, &idx);
  while (T--) {
    scanf("%d%d%d%d", &n, &m, &_c, &_f);
    rep(i, 1, n) {
      scanf("%s", s[i] + 1);
    }
    rep(i, 1, n) {
      int cnt = 0;
      per(j, m, 1) {
        if (s[i][j] == '0') {
          ri[i][j] = cnt;
          cnt += 1;
        } else {
          ri[i][j] = 0;
          cnt = 0;
        }
      }
    }
    rep(j, 1, m) {
      int sum = 0;
      rep(i, 1, n) {
        if (s[i][j] == '0') {
          Add(sum, ri[i][j]);
        } else {
          sum = 0;
        }
        a[i][j] = sum;
      }
      int cnt = 0;
      per(i, n, 1) {
        if (s[i][j] == '0') {
          dw[i][j] = cnt;
          cnt += 1;
        } else {
          dw[i][j] = 0;
          cnt = 0;
        }
      }
    }
    int ansc = 0, ansf = 0;
    rep(i, 3, n) {
      rep(j, 1, m) {
        if (s[i][j] == '0' && s[i - 1][j] == '0') {
          int t = mul(a[i - 2][j], ri[i][j]);
          Add(ansc, t);
          Add(ansf, mul(t, dw[i][j]));
        }
      }
    }
    printf("%d %d\n", ansc * _c, ansf * _f);
  }
  return 0;
}
