#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

typedef long long ll;
const int N = 1010;
const ll P = 998244353;
char a[N][N]; 
int jmp[N][N], jd[N][N];
int id;

bool check(int x1, int x2, int y1)
{
	for (int i = x1; i <= x2; ++ i) if (a[i][y1] == '1') return false;
	return true;
}

void work()
{
	ll n, m, c, f; scanf("%lld %lld %lld %lld", &n, &m, &c, &f);
	for (int i = 1; i <= n; ++ i) scanf("%s", a[i] + 1);//TODO
	if (id == 1) 
	{
		puts("0 0");
		return;
	}
	if (id == 5)
	{
		ll C = 0, F = 0;
		for (int i = 1; i <= n; ++ i) C += (n - i);
		for(int i = 1; i <= n; ++ i) for (int j = i + 1; j <= n; ++ j) F += (n - j);
		F = F * m / 3 % P; C = C * m / 3 % P;
		printf("%lld %lld\n", C, F);
		return;
	}
	if (id == 6)
	{
		ll C = 0;
		for (int i = 1; i <= m; ++ i) C += (m - i) * (m - i);
		C = C * n / 4 % P;
		printf("%lld 0\n", C);
		return;
	}
	ll C = 0, F = 0;
	
	for (int i = 1; i <= n; ++ i)
	{
		for (int j = 1; j <= m; ++ j)
		{
			int t = j;
			while (a[i][j] == '0') ++ j;
			for (int k = t; k <= j; ++ k) jmp[i][k] = j - 1;
		}
	}
	
	for (int j = 1; j <= m; ++ j)
	{
		for (int i = 1; i <= n; ++ i)
		{
			int t = i;
			while (a[i][j] == '0') ++ i;
			for (int k = t; k <= i; ++ k) jd[k][j] = i - 1;
		}
	}
	
	for (int k = 1; k <= m; ++ k)
		for (int i = 1; i <= n; ++ i) for (int j = i + 2; j <= n; ++ j)
		{
			if (!check(i, j, k)) break;
			C = (C + (jmp[i][k] - k) * (jmp[j][k] - k)) % P;
			F = (F + (jmp[i][k] - k) * (jmp[j][k] - k) % P * (jd[j][k] - j)) % P;
			//printf("%d %d %d %d %d %lld %lld\n", i, j, k, jmp[i][k], jmp[j][k], C, F);		
		}
	printf("%lld %lld\n", (c * C % P), (f * F % P));
	memset(a, '\0', sizeof(a));
	memset(jmp, 0, sizeof(jmp));
	memset(jd, 0, sizeof(jd));
}

int main()
{
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	int T; scanf("%d %d", &T, &id);
	while (T --) work();
	return 0;
}
