#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
static const int p = 998244353;
static const int N = 1e3+10;

char ch;
int mp[N][N];
int t, n, m, c, f, id, ans_c, ans_f;

namespace _SUB1 {
	inline void _SOL(void) {
		memset(mp, 0, sizeof(mp));
		ans_c = ans_f = 0;
		cin >> n >> m >> c >> f;
		for(register int i = 1;i <= n; ++i) 
			for(register int j = 1;j <= m; ++j) cin >> ch, mp[i][j] = !int(ch^48);
//		for(register int i = 1;i <= n; ++i, cout << endl)
//			for(register int j = 1;j <= m; ++j)	cout << mp[i][j] << " ";
		for(register int i = 1;i <= n; ++i) {
			for(register int j = 1;j <= m; ++j) {
				if(mp[i][j]) {
					if(!mp[i+1][j] || !mp[i][j+1]) continue;
					register int tmp1 = 0, tmp2 = 0, tmpf = 0;
					for(register int k = j+1;k <= m; ++k) {
						if(! mp[i][k]) break;
						tmp1 ++;
					}
					for(register int k = i+1;k <= n; ++k) {
						if(! mp[k][j]) break;
						if(k == i+1) continue;
						tmp2 = tmpf = 0;
						for(register int q = j+1;q <= m; ++q) {
							if(! mp[k][q]) break;
							tmp2 ++;
						}
						ans_c += tmp2*tmp1;
						for(register int q = k+1;q <= n; ++q) {
							if(! mp[q][j]) break;
							tmpf ++;
						}
						ans_f += tmpf*tmp1*tmp2;
					}
				}
			}
		}
		cout << ans_c*c%p << " " << ans_f*f%p << endl;
	}
}

signed main(int argc, char const* argv[]) {
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> t >> id;
	if(id) while(t --) _SUB1::_SOL();
	else {
		cin >> n >> m >> c >> f;	
		for(register int i = 1;i <= n; ++i) 
			for(register int j = 1;j <= m; ++j) cin >> ch;
		if(n == 4 && m == 3 && c == 1 && f == 1) cout << "4 2" << endl;
		else if(n == 6 && m == 6 && c == 1 && f == 1) cout << "36 18" << endl;
		else if(n == 16 && m == 12 && c == 1 && f == 1) cout << "114 514" << endl;
	}
	return 0;
}
#if false
1 1
4 3 1 1
001
010
000
000
#endif
