#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define int long long
const int mod = 998244353, N = 1e3 + 10;
int T, id, n, m, c, f, cnt_c, cnt_f;
char a[N][N];
int d[N][N], r[N][N];
int s_r[N][N], s_rd[N][N];
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	scanf ("%lld%lld", &T ,&id);
	while (T -- ) {
		
		memset(a, 0, sizeof a);
		memset(d, 0, sizeof d); memset(r, 0, sizeof r);	
		memset(s_r, 0, sizeof s_r); memset(s_rd, 0, sizeof s_rd);
		cnt_c = 0, cnt_f = 0;
		
		scanf ("%lld%lld%lld%lld", &n, &m, &c, &f);
		for (int i = 1; i <= n; i ++ ) scanf ("%s",a[i] + 1);
		for (int i = 1; i <= n; i ++ ) {
			int tmp = 0;
			for (int j = m; j >= 1; j --) {
				if (a[i][j] == '1') {
					tmp = 0; continue;
				}
				r[i][j] = tmp, tmp ++ ; 
			}
		} 
		for (int j = 1; j <= m; j ++ ) {
			int tmp = 0;
			for (int i = n; i >= 1; i -- ) {
				if (a[i][j] == '1') {
					tmp = 0; continue;
				}
				d[i][j] = tmp, tmp ++ ;
			}
		}
		
		
		for (int j = 1; j <= m; j ++ ) {
			int tmp = 0;
			for (int i = n; i >= 1; i -- ) {
				if (a[i][j] == '1') {
					tmp = 0;	continue;
				}
				s_r[i][j] = tmp, tmp += r[i][j] % mod, tmp %= mod;
			}
		}
		for (int j = 1; j <= m; j ++ ) {
			int tmp = 0;
			for (int i = n; i >= 1; i -- ) {
				if (a[i][j] == '1') {
					tmp = 0; continue;
				}
				s_rd[i][j] = tmp, tmp += r[i][j] * d[i][j] % mod, tmp %= mod; 
			}
		}
		
		
		for (int i = 1; i <= n; i ++ ) {
			for (int j = 1; j <= m; j ++ ) {
				if (a[i][j] == '1') continue;
				cnt_c += r[i][j] * s_r[i + 1][j] % mod, cnt_c %= mod;
				cnt_f += r[i][j] * s_rd[i + 1][j] % mod, cnt_f %= mod; 
			}
		}
		printf("%lld %lld\n", cnt_c * c % mod, cnt_f * f % mod);
	}
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/
