#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read() 
{
	int sum = 0, nega = 1; char ch = getchar();
	while(ch > '9' || ch < '0') {if(ch == '-') nega = -1; ch = getchar();}
	while(ch <= '9' && ch >= '0') sum = sum * 10 + ch - '0', ch = getchar();
	return nega * sum;
}
const int N = 1009, mod = 998244353;
int n, m, T, id, f, c, ansf, ansc;
char s[N][N];
int sx[N][N], sy[N][N]; //sx heng sy shu
inline void solve()
{
	n = read(), m = read(), c = read(), f = read();
	ansf = ansc = 0;
	for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
	for (int i = 0; i <= n + 4; i++) 
		for (int j = 0; j <= m + 4; j++) sx[i][j] = sy[i][j] = 0;
	for (int i = 1; i <= n; i++) 
		for (int j = m - 1; j >= 1; j--) 
			if(s[i][j + 1] == '0' && s[i][j] == '0') sx[i][j] = sx[i][j + 1] + 1;
			else sx[i][j] = 0;
	for (int i = 1; i <= m; i++)
		for (int j = n - 1; j >= 1; j--) 
			if(s[j + 1][i] == '0' && s[j][i] == '0') sy[j][i] = sy[j + 1][i] + 1;
			else sy[j][i] = 0;
	for (int i = 1; i <= m; i++)
	{
		int sum = 0;
		for (int j = 2; j <= n; j++) 
			if(s[j][i] == '0' && s[j - 1][i] == '0') 
			{
				ansc = (ansc + sum * sx[j][i] % mod) % mod;
				ansf = (ansf + sum * sx[j][i] % mod * sy[j][i]) % mod;
				sum = (sum + sx[j - 1][i]) % mod;
			}
			else sum = 0; 
	}
	ansc = ansc * c % mod; ansf = ansf * f % mod;
	cout << ansc << " " << ansf << endl;
}
signed main()
{
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	T = read(), id = read();
	for (int ttt = 1; ttt <= T; ttt++) solve();
	return 0;
}
