#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i) 
#define R(i, j, k) for(int i = (j); i >= (k); --i) 
#define ll long long 
#define vi vector < int > 
#define ull unsigned long long 
#define sz(a) ((int) (a).size())
#define me(a, x) memset(a, x, sizeof(a)) 
using namespace std;
const int N = 1007, mod = 998244353;
int n, m, c, f;
char s[N][N];
int tor[N][N];
void Main() {
	cin >> n >> m >> c >> f;
	L(i, 1, n) 
		cin >> (s[i] + 1);
	int nsc = 0, nsf = 0;
	me(tor, 0);
	L(i, 1, n) {
		int cnt = 0;
		R(j, m, 1) {
			if(s[i][j] == '0') ++cnt;
			else cnt = 0;
			tor[i][j] = cnt;
		}
	}
	L(j, 1, m) {
		int x = 0, z = 0;
//		L(i, 1, n) {
//			cout << tor[i][j] << ' ';
//		}
//		cout << '\n';
		L(i, 1, n) {
			if(!tor[i][j]) {
				x = z = 0;
				continue;
			}
			(nsc += (ll) x * (tor[i][j] - 1) % mod) %= mod;
			(nsf += z) %= mod;
			(z += (ll) x * (tor[i][j] - 1) % mod) %= mod;
			if(tor[i - 1][j]) (x += tor[i - 1][j] - 1) %= mod;
		}
//		cout << nsc << " and " << nsf <<'\n';
	}
	cout << (ll) nsc * c % mod << ' ' << (ll) nsf * f % mod << '\n';
}
int main() {
//	return system("fc plant1.out plant1.ans"), 0;
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t, tid;
	cin >> t >> tid;
	while(t--) Main();
	return 0;
}
