#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
const ll mod=998244353;
int n,m,C_res[N][N],C_sum[N][N],C,F,a[N][N],F_sum[N][N],mxd[N][N];
char s[N];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T,id;
	cin >> T >> id;
	while(T--){
		cin >> n >> m >> C >> F;
		memset(C_res,0,sizeof(C_res));
		memset(C_sum,0,sizeof(C_sum));
		memset(F_sum,0,sizeof(F_sum));
		memset(mxd,0,sizeof(mxd));
		memset(a,0,sizeof(a));
		for(int i=1;i<=n;++i){
			cin >> s+1;
			for(int j=1;j<=m;++j)
				a[i][j]=1-(s[j]-'0');
		}
		for(int i=n;i>=1;--i){
			int curl=0;
			for(int j=m;j>=1;--j){
				if(a[i][j]==0) curl=0;
				else curl++;
				C_res[i][j]=curl-1;
			}
		}
		for(int i=n;i>=1;--i){
			for(int j=1;j<=m;++j){
				if(a[i][j]==0) C_sum[i][j]=0;
				else C_sum[i][j]=(C_res[i][j]+C_sum[i+1][j])%mod;
			}
		}
		ll resc=0;
		for(int i=1;i<=n-2;++i){
			for(int j=1;j<=m;++j){
				if(a[i][j]==0||a[i+1][j]==0) continue;
				resc=(resc+1ll*(C_res[i][j])*(C_sum[i+2][j])%mod)%mod;//
			}
		}
		for(int i=1;i<=n;++i)
			for(int j=1;j<=m;++j){
				if(a[i][j]==0) F_sum[i][j]=0;
				else F_sum[i][j]=(F_sum[i-1][j]+C_res[i][j])%mod;
			}
		for(int i=n;i>=1;--i){
			for(int j=1;j<=m;++j){
				if(a[i][j]==0) mxd[i][j]=0;
				else mxd[i][j]=mxd[i+1][j]+1;
			}
		}
		ll resf=0;
		for(int i=3;i<=n;++i){
			for(int j=1;j<=m;++j){
				if(a[i][j]==0||a[i-1][j]==0||a[i-2][j]==0) continue;
				resf=(resf+1ll*(F_sum[i-2][j])*(C_res[i][j])%mod*(mxd[i][j]-1)%mod)%mod;
			}
		}
		cout << 1ll*C*resc%mod << ' ' << 1ll*F*resf%mod << '\n';
	}
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/