#include <bits/stdc++.h>

#define map ppdpdpdppdpd
using ll = long long;
const int mod = 998244353;
const int N = 1005;

int n, m, c, f, T, id;
char map[N][N];
int down[N][N];

ll fc[N];
void solve(ll& vc, ll& vf) {
  memset(down, 0, sizeof(down));
  memset(fc, 0, sizeof(fc));

  for (int i = n; i >= 1; i--)
    for (int j = 1; j <= m; j++)
      if (map[i][j] != '1')
        down[i][j] = down[i + 1][j] + 1;
  for (int i = 2; i < n; i++) {

    for (int j = m, len = 0; j >= 1; j--) {
      if (map[i - 1][j] == '1') len = 0; 
      else ++len;

      if (map[i][j] == '1') fc[j] = 0;
      else if (len > 1) (fc[j] += len - 1) %= mod;
    }

    for (int j = m, len = 0; j >= 1; j--) {
      if (map[i + 1][j] == '1') len = 0;
      else ++len;

      if (len > 1) {
        ll cnt = fc[j] * (len - 1) % mod;
        (vc += cnt) %= mod;
        (vf += down[i + 2][j] * cnt % mod) %= mod;
      }
    }
  }
}

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

  scanf("%d%d", &T, &id);

  while (T--) {
    scanf("%d%d%d%d", &n, &m, &c, &f);
    for (int i = 1; i <= n; i++)
      scanf("%s", map[i] + 1);
    ll vc = 0, vf = 0;
    solve(vc, vf);
    printf("%lld %lld\n", vc * c, vf * f);
  }

  return 0;
}