#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll mod = 998244353;
bool g[maxn][maxn];
ll dp[maxn][maxn], sum[maxn][maxn], down[maxn][maxn], up[maxn][maxn];
void solve()	{
	memset(dp, 0, sizeof(dp));
	memset(sum, 0, sizeof(sum));
	memset(down, 0, sizeof(down));
	memset(up, 0, sizeof(up));
	int n, m;
	ll c, f, ansc = 0, ansf = 0;
	string s;
	cin >> n >> m >> c >> f;
	for (int i = 1; i <= n; i++)	{
		cin >> s;
		for (int j = 0; j < m; j++)	
			g[i][j + 1] = s[j] == '1';
	}
	for (int i = 1; i <= n; i++)		{
		for (int j = m; j >= 1; j--)
			if (g[i][j] == 0)	
				dp[i][j] = dp[i][j + 1] + 1ll;
			else	dp[i][j] = 0;
	}
	for (int i = 1; i <= n;  i++)	{
		for (int j = 1; j <= m; j++)	{
			if (dp[i][j] > 1)	
				sum[i][j] = (sum[i - 1][j] + dp[i][j] - 1) % mod;
			else	sum[i][j] = sum[i - 1][j];
			if (g[i][j])	up[i][j] = 0;
			else	up[i][j] = up[i - 1][j] + 1;		
		}		
	}
	for (int i = n; i >= 1; i--)	{
		for (int j = 1; j <= m; j++)	{
			if (g[i][j])	down[i][j] = 0;
			else	down[i][j] = down[i + 1][j] + 1;
		}
	}
	for (int i = 1; i <= n; i++)	
		for (int j = 1; j <= m; j++)	{
			int t1 = down[i][j], t2 = up[i][j];
			if (t2 >= 3 && dp[i][j] >= 2 && g[i][j] == 0)	{
				ll temp = ((sum[i - 2][j] - sum[i - t2][j]) % mod + mod) % mod;		
				(ansc += (temp * (dp[i][j] - 1) % mod)) %= mod;			
			}
			if (t2 >= 3 && t1 >= 2 && g[i][j] == 0)	{
				ll temp = ((sum[i - 2][j] - sum[i - t2][j]) % mod + mod) % mod;
				(temp *= (dp[i][j] - 1)) %= mod;
				//printf("%lld ", temp);
				(ansf += temp * (t1 - 1)) %= mod;
				//printf("%d %d %lld\n", i, j, ansf);
			}
		}
	cout << (ansc * c) % mod << " " << (ansf * f) % mod << endl;
}
int main()	{
	ios::sync_with_stdio(false);
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T, id;
	cin >> T >> id;
	while (T--)	{
		solve();
	}
	return 0;
}