#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define Fin(x) freopen(x, "r", stdin)
#define Fout(x) freopen(x, "w", stdout)
#define Fio(x) Fin(x".in"), Fout(x".out")
using namespace std;
const int maxn = 1010, mod = 998244353;
int T, id, n, m, c, f, dw[maxn][maxn], r[maxn][maxn];
int sum[maxn][maxn], sum2[maxn][maxn];
char s[maxn][maxn];
signed main(){
	Fio("plant");
	scanf("%lld%lld", &T, &id);
	while(T--){
		scanf("%lld%lld%lld%lld", &n, &m, &c, &f);
		for(int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
		for(int i = 1; i <= n + 1; i++) for(int j = 1; j <= m + 1; j++) r[i][j] = dw[i][j] = sum[i][j] = sum2[i][j] = 0;
		for(int i = n; i >= 1; i--) for(int j = 1; j <= m; j++) if(s[i][j] != '1') dw[i][j] = dw[i + 1][j] + 1;
		for(int i = 1; i <= n; i++) for(int j = m; j >= 1; j--) if(s[i][j] != '1') r[i][j] = r[i][j + 1] + 1;
		for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) sum[i][j] = (sum[i - 1][j] + max(0ll, r[i][j] - 1)) % mod, sum2[i][j] = (1ll * i * max(0ll, r[i][j] - 1) % mod + sum2[i - 1][j]) % mod;
//		for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) printf("%lld %lld: %lld %lld\n", i, j, sum[i][j], sum2[i][j]);
		long long ansc = 0, ansf = 0;
		for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(r[i][j] > 1 && dw[i][j] > 2){
			r[i][j]--;
			long long res = 1ll * (sum[i + dw[i][j] - 1][j] - sum[i + 1][j] + mod) % mod;
			if(dw[i][j] >= 3) ansc = (ansc + res * r[i][j] % mod) % mod;
			if(dw[i][j] >= 4){
				res = (sum[i + dw[i][j] - 2][j] - sum[i + 1][j] + mod) % mod;//  long long tmp = ansf;
				ansf = (ansf + (1ll * res * (dw[i][j] + i - 1) % mod - (sum2[i + dw[i][j] - 2][j] - sum2[i + 1][j] + mod) % mod + mod) % mod * r[i][j] % mod) % mod;
//				printf("%lld %lld: %lld %lld\n", i, j, ansf - tmp, res);
			}
		}
		printf("%lld %lld\n", ansc * c, ansf * f);
	}
	return 0;
}

