#include<bits/stdc++.h>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define pt(x) cerr << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 1000 + 5, M = 1000 + 5, MOD = 998244353;
int testid, n, m, c, f, ansc, ansf, a[N][M];
char s[N][M];
int rb[N][M], up[N][M], dn[N][M];
void add(int &x, int y) {
	x = (x + y) % MOD;
}
void work() {
	cin >> n >> m >> c >> f;
	rep(i, 1, n) {
		scanf("%s", s[i] + 1);
		rep(j, 1, m) a[i][j] = s[i][j] - '0';
	}
	memset(rb, 0, sizeof(rb));
	memset(up, 0, sizeof(up));
	memset(dn, 0, sizeof(dn));
	ansc = ansf = 0;
	rep(i, 1, n) {
		rb[i][m+1] = m;
		per(j, m, 1) {
			rb[i][j] = (a[i][j] ? j - 1 : rb[i][j+1]);
		}
	}
	rep(j, 1, m) {
		dn[n+1][j] = n;
		per(i, n, 1) {
			dn[i][j] = (a[i][j] ? i - 1 : dn[i+1][j]);
		}
		rep(i, 1, n) {
			up[i][j] = (a[i][j] ? 0 : up[i-1][j] + (rb[i][j] - j));
			if(i >= 3 && !a[i][j] && !a[i-1][j] && !a[i-2][j]) {
				int resc = (ll)up[i-2][j] * (rb[i][j] - j) % MOD;
				add(ansc, resc);
				add(ansf, (ll)resc * (dn[i][j] - i) % MOD);
			}
		}
	}
	printf("%d %d\n", ansc * c, ansf * f);
}
int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T; cin >> T >> testid;
	while(T--) work();
	return 0;
}
/*
input
1 0
4 3 1 1
001
010
000
000
output
4 2
*/
