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

#define Reimu inline void
#define Marisa inline int
#define Sanae inline bool
#define Reisen inline LL

typedef long long LL;
typedef unsigned long long ULL;

typedef pair<int, int> Pii;
typedef tuple<int, int, int> Tiii;
#define fi first
#define se second

struct ModInt {
	static const int P = 998244353;
	static Marisa reduce(int x) { return x < P ? x : x - P; }

	int x;
	ModInt(int x = 0): x(x) {}

	friend ModInt operator-(ModInt a) { return reduce(P - a.x); }
	friend ModInt operator+(ModInt a, ModInt b) { return reduce(a.x + b.x); }
	friend ModInt operator-(ModInt a, ModInt b) { return reduce(P + a.x - b.x); }
	friend ModInt operator*(ModInt a, ModInt b) { return int(1LL * a.x * b.x % P); }

	friend ModInt &operator+=(ModInt &a, ModInt b) { return a = a + b; }
	friend ModInt &operator-=(ModInt &a, ModInt b) { return a = a - b; }
	friend ModInt &operator*=(ModInt &a, ModInt b) { return a = a * b; }

	friend ModInt qpow(ModInt a, int b = P - 2) { ModInt res = 1; for (; b; b >>= 1, a *= a) if (b & 1) res *= a; return res; }

	friend ModInt operator/(ModInt a, ModInt b) { return a * qpow(b); }
	friend ModInt &operator/=(ModInt &a, ModInt b) { return a *= qpow(b); }
	
	friend istream &operator>>(istream &cin, ModInt &a) { return cin >> a.x; }
	friend ostream &operator<<(ostream &cout, ModInt a) { return cout << a.x; }
};

const int N = 1010;

int n, m;
int a[N][N];
char s[N];
ModInt C, F;
ModInt f[N][N], g[N][N];

Reimu Sakuya() {
	cin >> n >> m >> C >> F;
	for (int i = 1; i <= n; ++i) {
		cin >> s + 1;
		for (int j = 1; j <= m; ++j) a[i][j] = s[j] & 1;
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = m; j; --j) {
			f[i][j] = a[i][j] ? 0 : f[i][j + 1] + 1;
			g[i][j] = a[i][j] ? 0 : g[i - 1][j] + f[i][j + 1];
		}
	}
	ModInt CC = 0, FF = 0;
	for (int j = 1; j <= m; ++j) {
		for (int i = n, len = 0; i > 1; --i) {
			if (a[i][j]) len = 0;
			else if (!a[i - 1][j]) {
				CC += (f[i][j] - 1) * (g[i - 1][j] - f[i - 1][j] + 1);
				FF += (f[i][j] - 1) * (g[i - 1][j] - f[i - 1][j] + 1) * len++;
			}
		}
	}
	cout << CC * C << ' ' << FF * F << '\n';
}

int main() {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int T, O; cin >> T >> O; while (T--) Sakuya();
	return 0;
}
