#include<iostream>
#include<cstdio>

#define int long long
using namespace std;

const int maxn = 1005,mo = 998244353;
int t,n,m,c,f,a[maxn][maxn],b[maxn][maxn];
int p[maxn][maxn],v[maxn][maxn],w[maxn][maxn];
int o;
char s[maxn];

signed main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%lld %lld",&t,&o);
	while (t--) {
		scanf("%lld %lld %lld %lld",&n,&m,&c,&f);
		for (int i=1; i<maxn; i++)
			for (int j=1; j<maxn; j++)
				a[i][j] = b[i][j] = p[i][j] = v[i][j] = w[i][j] = 0;
		for (int i=1; i<=n; i++) {
			scanf(" %s",s + 1);
			for (int j=1; j<=m; j++)
				if (s[j] == '1')
					a[i][j] = 1;
				else
					a[i][j] = 0;
		}


		if (c == 0 && f == 0) {
			printf("0 0\n");
			continue;
		}
		for (int i=1; i<=n; i++) {
			for (int j=m; j>=1; j--) {
				if (a[i][j] == 1)
					b[i][j] = 0;
				else
					b[i][j] = b[i][j + 1] + 1;
			}
			for (int j=1; j<=m; j++)
				if (b[i][j] > 0)
					b[i][j] -= 1;
		}

		for (int j=1; j<=m; j++) {
			for (int i=n; i>=1; i--)
				if (a[i][j] == 1)
					p[i][j] = 0,w[i][j] = 0;
				else
					p[i][j] = p[i + 1][j] + b[i][j],w[i][j] = w[i + 1][j] + 1;
			for (int i=n; i>=1; i--)
				if (w[i][j] > 0)
					w[i][j] -= 1;
		}

		for (int j=1; j<=m; j++)
			for (int i=n; i>=1; i--) {
				if (a[i][j] == 1)
					v[i][j] = 0;
				if (a[i][j] == 0)
					v[i][j] = (v[i + 1][j] + w[i][j] * b[i][j] % mo) % mo;

			}

		int sum1 = 0,sum2 = 0;

		for (int i=1; i<=n; i++)
			for (int j=1; j<=m; j++) {
				if (a[i][j] == 1 || a[i + 1][j] == 1)
					continue;
				sum1 = (sum1 + b[i][j] * p[i + 2][j] % mo) % mo;
				
				sum2 = (sum2 + v[i + 2][j] * b[i][j] % mo) % mo;
			}
		printf("%lld %lld\n",c * sum1 % mo,f * sum2 % mo);
	}

	return 0;
}