#include <bits/stdc++.h>
#define reopen(A) { freopen(#A".in", "r", stdin); freopen(#A".out", "w", stdout); }
#define F(i, l, r) for(int i = l; i < r; ++i)
#define Fe(i, l, r) for(int i = l; i <= r; ++i)
#define Fer(i, l, r) for(int i = l; i >= r; --i)
#define debug(a) cout<< #a": " << a <<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
constexpr int N = 1005, mod = 998244353;
int n, m, c, f;
bool a[N][N];
ll ha[N][N], le[N][N], lee[N][N];

signed main(){
	reopen(plant);
	ios::sync_with_stdio(0); cin.tie(0);
	int T, id;
	cin>>T>>id;
//	cout<<T<<endl;
	while(T--){
		memset(ha, 0, sizeof ha);
		memset(le, 0, sizeof le);
		memset(lee, 0, sizeof lee);
		ll ans1 = 0, ans2 = 0;
		cin>>n>>m>>c>>f;
//		cout<<n<<' '<<m<<' '<<c<<' '<<f<<endl;
		char tmp;
		Fe(i, 1, n) Fe(j, 1, m) {
			cin>>tmp;
			a[i][j] = tmp - '0';
		}
		if(id == 1){
			cout<< 0 << ' ' << 0 <<endl;
			break;
		}
		Fe(i, 1, n){
			Fer(j, m, 1){
				if(a[i][j] == 0) ha[i][j] = ha[i][j + 1] + 1;
				else ha[i][j] = 0;
			}
		}
		Fe(j, 1, m){
			Fer(i, n, 1){
				if(a[i][j] == 1 && a[i + 1][j] != 1 && a[i + 2][j] != 1) le[i][j] = i;
				else le[i][j] = le[i + 1][j];
			}
		}
		Fe(j, 1, m){
			Fer(i, n, 1){
				if(a[i][j] == 1) lee[i][j] = 0;
				else lee[i][j] = lee[i + 1][j] + 1;
			}
		}
		Fe(j, 1, m){
			ll t = 0, t2 = 0;
			Fe(i, 1, n){
				if(a[i][j] == 1 || a[i + 1][j] == 1) i = le[i][j] + 1;
				Fe(k, i + 2, n){
					if(a[k][j] == 1) {
						i = le[k][j] + 1;
						k = i + 2;
						if(k > n) break;
					}
					if(( ha[i][j] - 1 ) != 0 && ( ha[k][j] - 1 ) != 0){
						t = t + ( ha[i][j] - 1 ) * ( ha[k][j] - 1 ) % mod;	
						t %= mod;
						if(lee[k][j] > 1){
							 t2 = (t2 + ( ha[i][j] - 1 ) * ( ha[k][j] - 1 ) * (lee[k][j] - 1) % mod) % mod;
						}
					}
				}
			}
//			debug(t);
//			debug(t2);
			ans1 = (ans1 + t) % mod;
			ans2 = (ans2 + t2) % mod;
//			debug(ans2);
		}
		cout<< ans1 * c % mod <<' '<< ans2 * f % mod<<endl;
	}
	
	return 0;
}
