#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1005,P=998244353;
int n,m,a[N][N];
long long c[N][N],d[N][N];
signed main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	scanf("%lld%lld",&T,&id);
	while(T--) {
		memset(a,0,sizeof(a));
		memset(c,0,sizeof(c));
		memset(d,0,sizeof(d));
		int fc,ff;
		scanf("%lld%lld%lld%lld",&n,&m,&fc,&ff);
		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(j==m||a[i][j+1]==1||a[i][j]==1) c[i][j]=0;
				else c[i][j]=c[i][j+1]+1;
			}
		}
		for(int j=1;j<=m;j++) {
			int s=0;
			for(int i=n;i>=1;i--) {
				if(a[i][j]==1) {
					d[i][j]=0; s=0; continue;
				}
				d[i][j]=c[i][j]*s;
				s++;
			}
		}
		long long ansc=0,ansf=0;
		for(int j=1;j<=m;j++) {
			long long vc=0,vf=0,sc=0,sf=0;
			for(int i=1;i<=n;i++) {
				if(a[i][j]==1) {
					ansc=(ansc+vc)%P; ansf=(ansf+vf)%P;
					vc=vf=sc=sf=0;
					continue;
				}
				vc=(vc+sc*c[i][j])%P;
				sc=(sc+c[i][j])%P;
				vf=(vf+sf*d[i][j])%P;
				sf=(sf+c[i][j])%P;
			}
			ansc=(ansc+vc)%P; ansf=(ansf+vf)%P;
			for(int i=1;i<=n;i++) {
				if(a[i][j]==0&&a[i+1][j]==0) {
					ansc=(ansc-c[i][j]*c[i+1][j]%P+P)%P;
					ansf=(ansf-c[i][j]*d[i+1][j]%P+P)%P;
				}
			}
		}
		printf("%lld %lld\n",fc*ansc,ff*ansf);
	}
	return 0;
}
