#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const long long MOD = 998244353;
int T, id;
int n, m, c, f;
long long a[N][N], b[N][N], d[N][N];
char s[N][N];
int main()
{
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> T >> id;
	while(T --)
	{
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		cin >> n >> m >> c >> f;
		for (int i = 1; i <= n; ++ i) cin >> (s[i] + 1);
		for (int i = 1; i <= n; ++ i)
		{
			a[i][m + 1] = -1;
			for (int j = m; j; -- j)
			{
				a[i][j] = a[i][j + 1] + 1;
				if(s[i][j] == '1') a[i][j] = -1;
			}
		}
		for (int j = 1; j <= m; ++ j)
		{
			d[n + 1][j] = -1;
			for (int i = n; i; -- i)
			{
				d[i][j] = d[i + 1][j] + 1;
				if(s[i][j] == '1') d[i][j] = -1;
			}
		}
		for (int j = 1; j <= m; ++ j)
		{
			int tot = 0;
			for (int i = 1; i <= n; ++ i)
			{
				++ tot;
				if(s[i][j] == '1') tot = 0;
				if(tot >= 3) b[i][j] = b[i - 1][j] + a[i - 2][j];
			}
		}
		long long ans1 = 0, ans2 = 0;
		for (int j = 1; j <= m; ++ j)
		{
			long long qzh = 0;
			for (int i = n; i; -- i)
			{
				if(s[i][j] == '1') qzh = 0;
				else (qzh += -max(0ll, d[i - 1][j] - 1) * a[i - 1][j] % MOD + MOD) %= MOD, (ans1 += b[i][j] * a[i][j] % MOD) %= MOD, (ans2 += qzh * a[i][j] % MOD) %= MOD, (qzh += b[i][j]) %= MOD;
			}
		}
		cout << ans1 * c << " " << ans2 * f << "\n";
	}
}