#include <bits/stdc++.h>
using namespace std;
const int mod=998244353,N=1005;
int T,id,n,m,c,f,a[N][N],dis[N][N],tot,que[N],sum[N];
char s[N];
int Add(int x) { return x>=mod?x-mod:x; }
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while (T--) {
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for (int i=1; i<=n; i++) {
			scanf("%s",s+1);
			for (int j=1; j<=m; j++) a[i][j]=s[j]-'0';
			dis[i][m+1]=-1;
			for (int j=m; j>=1; j--)
				if (a[i][j]) dis[i][j]=-1;
				else dis[i][j]=dis[i][j+1]+1;
		}
		int ansc=0,ansf=0;
		for (int j=1; j<=m; j++)
			for (int l=1,r=1; r<=n; l=r+1,r=l) {
				if (a[r][j]) continue;
				while (r<n && !a[r+1][j]) r++;
				int tot=0; sum[0]=0;
				for (int i=l; i<=r; i++) {
					que[++tot]=dis[i][j];
					sum[tot]=Add(sum[tot-1]+que[tot]);
				}
				for (int i=2; i<=tot; i++)
					ansc=(1ll*sum[i-2]*que[i]+ansc)%mod,
					ansf=(1ll*sum[i-2]*que[i]%mod*(tot-i)+ansf)%mod;
			}
		printf("%d %d\n",c*ansc,f*ansf);
	}
	return 0;
}
