#include <bits/stdc++.h>
#define ll long long
#define re register
using namespace std;
inline int read() {int ret = 0, flag = 1;char ch = getchar();while(ch < '0' || ch > '9') {if(ch == '-') flag = -1;ch = getchar();}while(ch >= '0' && ch <= '9') {ret = ret * 10 + ch - '0';ch = getchar();} return ret * flag;}
const int N = 1e3 + 15;
const int mod = 998244353;

int mp[N][N];

int sumr[N][N], sumd[N][N], ssumd[N][N];
int sr[N][N], s[N][N], ss[N][N];

int n, m, cc, ff;
int ans1, ans2;

void solve() {
	int cnt = 0;
	int ret = 0;
	for (re int i=1; i<=n; i++) {
		for (re int j=1; j<=m; j++) {
			if(sumd[i][j] < 3) continue;
			int tmp = ssumd[i+2][j] - sumd[i+2][j];
			cnt = (1ll * cnt + 1ll * (sumr[i][j] - 1) * tmp % mod) % mod;
		}
	}
	
	for (re int i=1; i<=n; i++) {
		for (re int j=1; j<=m; j++) {
			if(sumd[i][j] < 4) continue;
			int tmp = ss[i+2][j] - s[i+2][j];
			ret = (1ll * ret + 1ll * (sumr[i][j] - 1) * tmp % mod) % mod;
		}
	}
	ans1 = cnt * cc;
	ans2 = ret * ff;
}


char iin[N];
int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T = read(); 
	int id = read();
	while(T --) {
		memset(s, 0, sizeof(s));
		memset(ss, 0, sizeof(ss));
		memset(sr, 0, sizeof(sr));
		memset(sumr, 0, sizeof(sumr));
		memset(sumd, 0, sizeof(sumd));
		memset(ssumd, 0, sizeof(ssumd));
		n = read(); m = read(); cc = read(); ff = read();
		for (re int i=1; i<=n; i++) {
			scanf("%s", iin+1);
			for (re int j=1; j<=m; j++) {
				mp[i][j] = 1 - (iin[j] - '0');
			}
		}
		for (re int i=1; i<=n; i++) {
			for (re int j=m; j>=1; j--) {
				if(!mp[i][j]) sumr[i][j] = 0;
				else {
					sumr[i][j] = sumr[i][j+1] + 1;
				}
			}
		}
		
		for (re int i=n; i>=1; i--) {
			for (re int j=1; j<=m; j++) {
				if(!mp[i][j]) {
					sumd[i][j] = ssumd[i][j] = 0;
				} else {
					sumd[i][j] = sumd[i+1][j] + 1;
					ssumd[i][j] = ssumd[i+1][j] + sumr[i][j];
					
				}
			}
		}
		
		for (re int i=1; i<=n; i++) {
			for (re int j=m; j>=1; j--) {
				if(!mp[i][j]) sr[i][j] = 0;
				else sr[i][j] = sumr[i][j] * (sumd[i][j] - 1);
			}
		}
		
		for (re int i=n; i>=1; i--) {
			for (re int j=1; j<=m; j++) {
				if(!mp[i][j]) {
					s[i][j] = ss[i][j] = 0;
				} else {
					s[i][j] = s[i+1][j] + sumd[i][j] - 1;
					ss[i][j] = ss[i+1][j] + sr[i][j];
				}
			}
		}
		
		/*for (re int i=1; i<=n; i++) {
			for (re int j=1; j<=m; j++) {
				printf("%d ", sumr[i][j]);
			}
			puts("");
		}*/
		
		solve();
		
		printf("%d %d\n", ans1, ans2);
	}
	return 0;
}
