#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
constexpr int kMod = 998244353;
std::ifstream fin("plant.in");
std::ofstream fout("plant.out");
int T, id, n, m, C, F, r[1005][1005], d[1005][1005], s[1005][1005],
    s2[1005][1005];
char a[1005][1005];
int add(int x, int y) { return x + y >= kMod ? x + y - kMod : x + y; }
void Add(int &x, int y) {
  if ((x += y) >= kMod) x -= kMod;
}
int sub(int x, int y) { return x < y ? x - y + kMod : x - y; }
int mul(int x, int y) { return (long long)x * y % kMod; }
void Solve() {
  std::cin >> n >> m >> C >> F;
  for (int i = 1; i <= n; i++) std::cin >> (a[i] + 1);
  std::memset(r, 0, sizeof(r)), std::memset(d, 0, sizeof(d));
  std::memset(s, 0, sizeof(s)), std::memset(s2, 0, sizeof(s2));
  for (int i = 1; i <= n; i++)
    for (int j = m; j >= 1; j--)
      if (a[i][j] == '0') r[i][j] = r[i][j + 1] + 1;
  for (int i = n; i >= 1; i--)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == '0') d[i][j] = d[i + 1][j] + 1;
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= n; i++) s[i][j] = add(s[i - 1][j], r[i][j] - 1);
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= n; i++) {
      int f = 0;
      if (a[i][j] == '0') f = mul(r[i][j] - 1, d[i][j] - 1);
      s2[i][j] = add(s2[i - 1][j], f);
    }
  int c = 0, f = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '1') continue;
      int D = i + d[i][j] - 1;
      if (d[i][j] >= 3) Add(c, mul(r[i][j] - 1, sub(s[D][j], s[i + 1][j])));
      if (d[i][j] >= 4) Add(f, mul(r[i][j] - 1, sub(s2[D][j], s2[i + 1][j])));
    }
  std::cout << mul(C, c) << ' ' << mul(F, f) << '\n';
}
int main(int argc, char const *argv[]) {
  // freopen("plant.in", "r", stdin);
  std::cin.rdbuf(fin.rdbuf()), std::cout.rdbuf(fout.rdbuf());
  std::cin.tie(nullptr);
  std::cin >> T >> id;
  while (T--) Solve();
  return 0;
}