#include <iostream>
using namespace std;

typedef long long LL;
const int N = 1010, mod = 998244353;

int T, id;
int a[N][N];
int l[N][N], q[N][N];
int C[N][N], F[N][N];

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d", &T, &id);
	while (T --){	
		memset(a, 0, sizeof a);
		memset(l, 0, sizeof l);
		memset(q, 0, sizeof q);
		memset(C, 0, sizeof C);
		memset(F, 0, sizeof F);
		int n, m, c, f;
		scanf("%d%d%d%d", &n, &m, &c, &f);
		for (int i = 1;i <= n; ++ i){
			string s;
			cin >> s;
			for (int j = 1;j <= m; ++ j)
				a[i][j] = s[j - 1] - '0';
		}

		for (int i = n;i >= 1; -- i)
			for (int j = m;j >= 1; -- j){
				if (a[i][j]) continue;
				l[i][j] = l[i][j + 1] + 1;
				q[i][j] = q[i + 1][j] + 1;
				C[i][j] = (l[i][j] - 1 + C[i + 1][j]) % mod;
				F[i][j] = (1LL * (l[i][j] - 1) * (q[i][j] - 1) + F[i + 1][j]) % mod;
			}

		LL ansc = 0, ansf = 0;
		for (int i = 1;i <= n - 1; ++ i)
			for (int j = 1;j <= m - 1; ++ j){
				if (l[i][j] <= 1) continue;
				if (a[i + 1][j]) continue;
				ansc = (ansc + 1LL * (l[i][j] - 1) * C[i + 2][j]) % mod;
				ansf = (ansf + 1LL * (l[i][j] - 1) * F[i + 2][j]) % mod;
			}
		ansc *= c, ansf *= f;
		printf("%lld %lld\n", ansc, ansf);
	}
	return 0;
}