#include<iostream>
#include<cstring>
const int sz = 1010;
using ll = long long;
const int mod = 998244353;
int map[sz][sz], bottom[sz][sz], right[sz][sz], n, m;
bool vis[sz][sz];
void dfs(int x, int y) {
if (vis[x][y]) return;
vis[x][y] = true;
if (x + 1 <= n) dfs(x + 1, y);
if (y + 1 <= m) dfs(x, y + 1);
if (map[x][y]) return bottom[x][y] = right[x][y] = -1, void();
bottom[x][y] = bottom[x + 1][y] + 1;
right[x][y] = right[x][y + 1] + 1;
}
int main() {
std::ios::sync_with_stdio(false);
int t, id;
std::cin >> t >> id;
while (t--) {
int c, f;
std::cin >> n >> m >> c >> f;
char ch;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
std::cin >> ch, map[i][j] = ch - '0';
memset(vis, 0, sizeof vis);
memset(right, 0xff, sizeof right);
memset(bottom, 0xff, sizeof bottom);
dfs(1, 1);
ll ansc = 0, ansf = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (right[i][j] <= 0) continue;
for (int k = 2; k <= bottom[i][j]; k++) {
if (right[i + k][j] > 0) {
ansc = (ansc + right[i + k][j] * right[i][j]) % mod;
ansf = (ansf + bottom[i + k][j] * right[i + k][j] * right[i][j]) % mod;
}
}
}
}
std::cout << ansc * c % mod << " " << ansf * f % mod << "\n";
}
return 0;
}