#include<bits/stdc++.h>
#define int long long //__int128
const int MAXN = 1e3+3;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;//0x3f3f3f3f3f3f3f3f;
using namespace std;
const int mod = 998244353;
int a[MAXN][MAXN],id,n,m,c,f;
int R[MAXN][MAXN];
int D[MAXN][MAXN];
int SR[MAXN][MAXN];
int SJ[MAXN][MAXN];
//duo ce bu qing
inline int querySR(int x,int y) {
	int r = min(x+D[x][y]-1,n);
	int l = x+2;
	if(l>r)return 0;
	return ((SR[r][y]-SR[l-1][y])%mod+mod)%mod;
}
inline int querySJ(int x,int y) {
	int r = min(x+D[x][y]-2,n);
	int l = x+2;
	if(l>r)return 0;
	return ((SJ[r][y]-SJ[l-1][y])%mod+mod)%mod;
}
int C = 0,F = 0;
void work() {
	cin>>n>>m>>c>>f;
	C =0,F=0;
	memset(R,0,sizeof R);
	memset(SR,0,sizeof SR);
	memset(D,0,sizeof D);
	memset(SJ,0,sizeof SJ);
	memset(a,0,sizeof a);
	for(int i = 1; i<=n; i++)
		for(int j = 1; j<=m; j++)
			scanf("%1lld",&a[i][j]);
	for(int i = 1; i<=n; i++) {
		for(int j = m; j>=1; j--) {
			if(a[i][j] == 1)R[i][j] = 0;
			else R[i][j] = R[i][j+1]+1,R[i][j]%=mod;
		}
	}
	for(int j = 1; j<=m; j++) {
		for(int i = n; i>=1; i--) {
			if(a[i][j]==1)D[i][j] = 0;
			else D[i][j] = D[i+1][j]+1,D[i][j]%=mod;
		}
	}
	for(int i = 1; i<=n; i++) {
		for(int j = 1; j<=m; j++) {
			if(R[i][j]>=2)
				SR[i][j] = (SR[i-1][j]+R[i][j]-1)%mod;
			else SR[i][j] = SR[i-1][j];
		}
	}
	for(int i = 1; i<=n; i++) {
		for(int j = 1; j<=m; j++) {
			if(D[i][j]>=2&&R[i][j]>=2) {
				SJ[i][j] = (SJ[i-1][j]+(R[i][j]-1)%mod*(D[i][j]-1)%mod)%mod;
			} else {
				SJ[i][j] = SJ[i-1][j];
			}
		}
	}
	for(int j = 1; j<=m; j++) {
		for(int i = 1; i<=n; i++) {
			if(R[i][j]>=2&&i+1!=n&&a[i+1][j]!=1&&i!=n) {
				C+=querySR(i,j)*(R[i][j]-1)%mod,C%=mod;
			}
		}
	}
	for(int j = 1; j<=m; j++) {
		for(int i = 1; i<=n; i++) {
			if(R[i][j]>=2&&D[i][j]!=1&&i+1!=n&&a[i+1][j]!=1&&i!=n) {
				F+=querySJ(i,j)*(R[i][j]-1)%mod,F%=mod;
			}
		}
	}
	cout<<((c%mod*C)%mod+mod)%mod<<" "<<((f%mod*F)%mod+mod)%mod<<"\n";
	return;
}
signed main() {
//	ios::sync_with_stdio(0);
//	cin.tie(0);
//	cout.tie(0);
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T;
	cin>>T>>id;
	while(T--) {
		work();
	}
	return 0;
}


