#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
const int P=998244353;

int T,id;
int n,m;
ll c,f;
int a[N][N];
ll sum[N][N],nex[N];
ll summul[N],summul2[N];

void solve(){
	scanf("%d%d%lld%lld",&n,&m,&c,&f);
	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++){
		sum[i][m+1]=0;
		for(int j=m;j>=1;j--){
			if(a[i][j]==1) sum[i][j]=0;
			else sum[i][j]=sum[i][j+1]+1;
		}
	}
	ll ansc=0,ansf=0;
	for(int j=1;j+1<=m;j++){
		nex[n+1]=0;
		for(int i=n;i>=1;i--){
			if(a[i][j]==1) nex[i]=0;
			else{
				if(nex[i+1]==0) nex[i]=i;
				else nex[i]=nex[i+1];
			}
		}
		summul[0]=1;summul2[0]=1;
		for(int i=1;i<=n;i++){
			summul[i]=summul[i-1]+max(sum[i][j]-1,0LL)%P;
			summul2[i]=summul2[i-1]+max((sum[i][j]-1)*(nex[i]-i),0LL)%P;
		}
		
		for(int i=1;i+2<=n;i++){
			if(a[i][j]||a[i+1][j]||a[i+2][j]) continue;
			if(sum[i][j]<=1) continue;
			ansc+=(sum[i][j]-1)*(summul[nex[i]]-(summul[i+1]));
			ansf+=(sum[i][j]-1)*(summul2[nex[i]]-(summul2[i+1]));
			ansc%=P;ansf%=P;
//			printf("i:%d:%lld(%lld num:%lld[%d %d])\n",i,ansc,nex[i],summul[nex[i]]-(summul[i+1]),summul[nex[i]],(summul[i+1]));
		}
//		printf("j%d:%lld\n",j,ansc);
	}
	
	ansc*=c;ansf*=f;
	ansc%=P;ansf%=P;
	printf("%lld %lld\n",ansc,ansf);
}

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--)
		solve();
	return 0;
}
