/*---------------------
  auther: nomonick
  contest: NOIP
  task name: plant
  code time: 2022/11/26 09:07
  E(T1): 80
  rp++
---------------------*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int SIZE = (int)1e3 + 50;
const int mod = 998244353;

inline int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' or ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while ('0' <= ch and ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f * x;
}

inline void write(int x)
{
	if (x < 0) putchar('-'), x *= -1;
	if (x > 9) write(x / 10), putchar((x % 10) + '0');
	else putchar(x % 10 + '0');
}

int T,id;
int n,m,c,f,ans1,ans2;
int sum[SIZE][SIZE],num[SIZE][SIZE];
char str[SIZE][SIZE];

inline void solve()
{
	n = read(); m = read(); c = read(); f = read();
	for (int i = 1; i <= n; ++i) scanf("%s",str[i]+1);
	if (c == f and c == 0) return write(0), putchar(' '), write(0), puts(""), void();
	for (int i = 1; i <= n; ++i)
	{
		for (int j = m; j; --j)
		{
			if (str[i][j] == '1') sum[i][j] = 0;
			else sum[i][j] = sum[i][j+1] + 1;
		}
	}
	for (int j = 1; j <= m; ++j)
	{
		for (int i = n; i; --i)
		{
			if (str[i][j] == '1') num[i][j] = 0;
			else num[i][j] = num[i+1][j] + 1;
		}
	}
	for (int j = 1; j <= m and c; ++j)
	{
		for (int i = 1; i <= n; ++i)
		{
			if (num[i][j] <= 2) continue;
			if (sum[i][j] <= 1) continue;
			for (int k = i + 2; k < i + num[i][j] and k <= n; ++k)
			{
				if (sum[k][j] <= 1) continue;
				ans1 = (ans1 + (sum[i][j] - 1 + mod) % mod * (sum[k][j] - 1 + mod) % mod) % mod;
			}
		}
	}
	for (int j = 1; j <= m and f; ++j)
	{
		for (int i = 1; i <= n; ++i)
		{
			if (num[i][j] <= 2) continue;
			if (sum[i][j] <= 1) continue;
			for (int k = i + 2; k < i + num[i][j] - 1 and k <= n; ++k)
			{
				if (i == 4 and j == 4) cout << k << endl;
				if (sum[k][j] <= 1) continue;
				// cout << "(" << i << "," << j << ")" << " (" << k << "," << j << ")" << endl;
				ans2 = (ans2 + (sum[i][j] - 1 + mod) % mod * (sum[k][j] - 1 + mod) % mod * (num[i][j] + i - 1 - k) % mod) % mod;
			}
		}
	}
	ans1 = ans1 * c % mod; ans2 = ans2 * f % mod;
	write(ans1); putchar(' '); write(ans2); puts("");
}

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T = read(); id = read();
	for (; T; --T) solve();
	return 0;
}

// plant3.ans = 114 514 
// ????? black man with ? ?????