#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long ll;
const int N=1145;
const ll P=998244353;

int T,id,n,m,g[N][N];
ll C,F,ansc,ansf,d[N][N],s[N][N],b[N][N];
string st;

ll read(){
	ll x=0,c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') x=x*10+c-48,c=getchar();
	return x;
}

void allclear(){
	memset(g,0,sizeof(g));
	memset(d,0,sizeof(d));
	memset(s,0,sizeof(s));
	memset(b,0,sizeof(b));
	ansc=0ll;ansf=0ll;
}

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read();id=read();
	while (T--){
		allclear();
		n=read();m=read();C=read();F=read();
		for (int i=1;i<=n;i++){
			cin>>st;
			for (int j=1;j<=m;j++)
				g[i][j]=st[j-1]-48;
		}
		for (int i=1;i<=n;i++){
			d[i][m+1]=0;
			for (int j=m;j>=1;j--){
				if (g[i][j]) d[i][j]=0;
				else d[i][j]=d[i][j+1]+1;
			}
		}
		for (int j=1;j<=m;j++){
			s[0][j]=0;
			for (int i=1;i<=n;i++){
				if (g[i][j]) s[i][j]=0;
				else s[i][j]=s[i-1][j]+d[i][j]-1;
			}
		}
		for (int i=3;i<=n;i++)
			for (int j=1;j<m;j++){
				if (g[i][j] || g[i-1][j]) continue;
				ansc=(ansc+s[i-2][j]*d[i][j+1]%P)%P;
			}
		for (int j=1;j<=m;j++){
			b[n+1][j]=0;
			for (int i=n;i>=1;i--){
				if (g[i][j]) b[i][j]=0;
				else b[i][j]=b[i+1][j]+1;
			}
		}
		for (int i=3;i<n;i++)
			for (int j=1;j<m;j++){
				if (g[i][j] || g[i-1][j]) continue;
				ansf=(ansf+s[i-2][j]*d[i][j+1]%P*(b[i][j]-1)%P)%P;
			}
		printf("%lld %lld\n",ansc*C%P,ansf*F%P);
	}
	return 0;
}
