#include<bits/stdc++.h>
using namespace std;

const int maxn = 1005;
const int mod = 998244353;

int T;
int id;
int n, m, c, f;
int a[maxn][maxn];
int r[maxn][maxn];
int u[maxn][maxn];
int ansc, ansf;

int main()
{
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	cin >> T;
	cin >> id;
	while (T--)
	{
		cin >> n >> m >> c >> f;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				char x;
				cin >> x;
				a[i][j] = x - '0';
			}
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = m; j > 0; j--)
			{
				if (a[i][j] == 0)
				{
					r[i][j] = r[i][j + 1] + 1;
				}
			}
		}
		for (int i = n; i > 0; i--)
		{
			for (int j = 1; j <= m; j++)
			{
				if (a[i][j] == 0)
				{
					u[i][j] = u[i + 1][j] + 1;
				}
			}
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (a[i][j] == 0)
				{
					int sumc = 0;
					for (int k = 2; k < u[i][j]; k++)
					{
						sumc += r[i + k][j] - 1;
						sumc %= mod;
					}
					ansc += sumc * (r[i][j] - 1);
					ansc %= mod;
					
					int sumf = 0;
					for (int k = 2; k < u[i][j] - 1; k++)
					{
						sumf += (r[i + k][j] - 1) * (u[i + k][j] - 1);
						sumf %= mod;
					}
					ansf += sumf * (r[i][j] - 1);
					ansf %= mod;
				}
			}
		}
		cout << ansc * c << " " << ansf * f;
	}
	return 0;
}
