#include<bits/stdc++.h>
#define N 1005
#define mod 998244353
#define int long long
using namespace std;
int n,m,C,F,ac,af,a[N][N],d[N][N],r[N][N],p[N][N],l[N][N];
char s[N];
inline int read(){
	int res=0;char ch=getchar();int f=ch=='-'?-1:1;
	while(ch<'0'||ch>'9')ch=getchar(),f=ch=='-'?-1:f;
	while(ch>='0'&&ch<='9')res=res*10+ch-'0',ch=getchar();
	return res*f;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
    int T=read(),id=read();
    while(T--){
    	n=read(),m=read(),C=read(),F=read();
    	memset(d,0,sizeof(d)),memset(r,0,sizeof(r));
    	memset(p,0,sizeof(p)),memset(l,0,sizeof(l));
    	ac=af=0;
	    for(int i=1;i<=n;++i){
	    	scanf("%s",s+1);
	    	for(int j=1;j<=m;++j)
	    	a[i][j]=s[j]=='1';
		}
	    for(int i=1;i<=n;++i)
	    for(int j=m;j;--j)
	    if(!a[i][j])r[i][j]=r[i][j+1]+1,r[i][j]%=mod;
	    for(int i=1;i<=m;++i)
	    for(int j=n;j;--j)if(!a[j][i]){
	    	l[j][i]=l[j+1][i]+1,l[j][i]%=mod;
            d[j][i]=d[j+1][i]+r[j][i]-1,d[j][i]%=mod;
            p[j][i]=p[j+1][i]+(r[j][i]-1)*(l[j][i]-1)%mod,p[j][i]%=mod;
		    if(!a[j+1][i])
			ac+=(r[j][i]-1)*d[j+2][i]%mod,ac%=mod,
		    af+=(r[j][i]-1)*p[j+2][i]%mod,af%=mod;
        }
	    printf("%lld %lld\n",ac*C,af*F);
	}
	return 0;
}
