#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pa pair<long long,long long>
#define mp make_pair
#define fi first
#define se second

const ll P=998244353;const int N=1e3+5;
int T,id,n,m,i,j,a[N][N];
ll c,f,ansc,ansf,r[N][N],d[N][N],sum1[N][N],sum2[N][N],b[N];
char ch;
inline ll read(){
	ll x=0,f=1;register char c=getchar();
	while(c<'0'||c>'9') f=(c=='-'?-1:1),c=getchar();
	while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
	return x*f;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read();id=read();
	while(T--){
		n=read();m=read();c=read();f=read();
		for(i=1;i<=n;++i){
			while(ch<'0'||ch>'9') ch=getchar();
			for(j=1;j<=m;++j){
				if(ch=='1') a[i][j]=1;
				if(i<n||j<m) ch=getchar();
			}
		}
		for(i=1;i<=n;++i)
			for(j=m;j>0;--j)
				if(a[i][j])
				    r[i][j]=0;else
				    r[i][j]=r[i][j+1]+1;
		for(j=1;j<=m;++j)
		    for(i=n;i>0;--i)
		        if(a[i][j])
		            d[i][j]=0;else
		            d[i][j]=d[i+1][j]+1;
		for(j=1;j<=m;++j)
		    for(i=n;i>0;--i)
		        sum1[i][j]=(sum1[i+1][j]+r[i][j]-1+P)%P;
		for(j=1;j<=m;++j)
		    for(i=n;i>0;--i)
		        sum2[i][j]=(sum2[i+1][j]+r[i][j]*i%P)%P;
		for(i=1;i<=n;++i) b[i]=(b[i-1]+i)%P;
		for(i=1;i<=n-2;++i)
		    for(j=1;j<=m;++j)
		        if(d[i][j]>2){
		    	    ansc=(ansc+(r[i][j]-1)*(sum1[i+2][j]-sum1[i+d[i][j]][j]))%P;
		    	    if(d[i][j]>3)
		    	        ansf=(ansf+(r[i][j]-1)*((i+d[i][j]-1)*(sum1[i+2][j]-sum1[i+d[i][j]][j])%P+(b[i+d[i][j]-1]-b[i+1])+P%P-(sum2[i+2][j]-sum2[i+d[i][j]][j])%P+P))%P;
			    }
		printf("%lld %lld\n",ansc*c%P,ansf*f%P);
	}
	return 0;
}
