#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll M = 998244353;
int T, id, n, m, c, f;
ll sc, sf;
char a[1010][1010];
ll nxt_col[1010][1010], nxt_row[1010][1010];

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> T >> id;
	while (T--) {
		cin >> n >> m >> c >> f;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				cin >> a[i][j];
		memset(nxt_col, 0, sizeof nxt_col);
		memset(nxt_row, 0, sizeof nxt_row);
		sc = sf = 0;
		for (int i = 1; i <= n; i++)
			for (int j = m; j; j--)
				if (a[i][j] == '1')
					nxt_col[i][j] = 0;
				else
					nxt_col[i][j] = nxt_col[i][j + 1] + 1;
		for (int j = 1; j <= m; j++)
			for (int i = n; i; i--)
				if (a[i][j] == '1')
					nxt_row[i][j] = 0;
				else
					nxt_row[i][j] = nxt_row[i + 1][j] + 1;
		for (int j = 1; j <= m; j++) {
			ll tot = 0;
			for (int i = 3; i <= n; i++) {
				if (a[i - 2][j] == '0')
					tot = (tot + nxt_col[i - 2][j + 1]) % M;
				else
					tot = 0;
				if (a[i][j] == '0' && a[i - 1][j] == '0') {
					sc = (sc + tot * nxt_col[i][j + 1]) % M;
					sf = (sf + tot * nxt_col[i][j + 1] % M * nxt_row[i + 1][j]) % M;	
				}
			}
		}
		cout << c * sc % M << ' ' << f * sf % M << '\n';
	}
	return 0;
}