#include<bits/stdc++.h>
using namespace std;
#define N 1003
#define ll long long
int n, m, T, id, c, f;
string s[N];
ll rt[N][N], down[N][N], ans1, ans2;
ll mod = 998244353;
int main(){
	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);
	cin>>T>>id;
	while(T--){
		cin>>n>>m>>c>>f;
		memset(rt, 0, sizeof(rt));
		memset(down, 0, sizeof(down));
		for(int i=1;i<=n;i++) cin>>s[i];
		for(int i=n;i>=1;i--){
			for(int j=m;j>=1;j--){
				if(s[i][j-1] == '0'){
					rt[i][j] = rt[i][j+1] + 1;
					down[i][j] = down[i+1][j] + 1;
				}
			}
		}
		ans1 = ans2 = 0;
		for(int j=1;j<=m;j++){
			int l = 0, r = 0;
			ll sum1 = 0, sum2 = 0;
			while(l<n+1 && r<n+1){  // new turn
			    while(l <= n+1 && rt[l][j] == 0) l ++;  // find first
			    if(l > n+1) continue;
			    r = l + 1;
			    while(r < n+1 && rt[r][j]) {
			    	sum1 += rt[r][j] - 1;
			    	sum1 %= mod;
			    	r ++;
				}
				int cnt = 0;
				for(int x = r-1;x > l;x --){
					sum2 += cnt * (rt[x][j] - 1) % mod;
					sum2 %= mod;
					cnt ++;
				}
				while(l < r - 1){
					sum1 -= rt[l+1][j] - 1; sum1 %= mod;
					ans1 += (rt[l][j] - 1) * sum1 % mod; ans1 %= mod;
					sum2 -= (r - l - 2) * (rt[l+1][j] - 1) % mod; sum2 %= mod;
					ans2 += (rt[l][j] - 1) * sum2 % mod; ans2 %= mod;
					l ++;
				}
				l = r;
			}
		}
		ans1 = ans1 * c % mod; ans2 = ans2 * f % mod;
		ans1 = (ans1 + mod) % mod; ans2 = (ans2 + mod) % mod;
		printf("%lld %lld\n", ans1 * c % mod, ans2 * f % mod);
	}
}
