#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];
//duo ce bu qing
int C = 0,F = 0;
void work() {
	cin>>n>>m>>c>>f;
	memset(R,0,sizeof R);
	memset(D,0,sizeof D);
	memset(a,0,sizeof a);

	for(int i = 1; i<=n; i++)
		for(int j = 1; j<=m; j++)
			scanf("%1d",&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;
		}
	}
	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;
		}
	}
	
	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)) {
				for(int k = i+2; k<=n; k++) {
					if(a[k][j]==1)break;
					if(R[k][j]>=2) {
						C+=(R[i][j]-1)*(R[k][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) {
				for(int k = i+2; k<=n; k++) {
					if(a[k][j]==1)break;
					if(D[k][j]>1&&R[k][j]>=2) {
						F+=(R[i][j]-1)*(R[k][j]-1)*(D[k][j]-1)%mod,F%=mod;
					}
				}
			}
		}
	}
	cout<<(c%mod*C)%mod<<" "<<(f%mod*F)%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;
}
