#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    char c=getchar();
    int x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) x=x*10+(c&15),c=getchar();
    return x;
}
const int p=998244353;
char s[1003][1003];
int a[1003][1003],b[1003][1003],f[1003][1003];
signed main()
{
#ifndef local
    freopen("plant.in","r",stdin),
    freopen("plant.out","w",stdout);
#endif
	int T=read(),id=read();
	while(T--)
	{
		int n=read(),m=read(),mulc=read(),mulf=read(),ansc=0,ansf=0;
		for(int i=0; i<=n+1; ++i)
			memset(a[i],0,(m+2)<<2),
			memset(b[i],0,(m+2)<<2),
			memset(f[i],0,(m+2)<<2);
		for(int i=1; i<=n; ++i)
			scanf("%s",s[i]+1);
		for(int i=1; i<=n; ++i)
			for(int j=m; j>=1; --j)
				if(s[i][j]=='0') a[i][j]=a[i][j+1]+1;
		for(int i=1; i<=n; ++i)
			for(int j=1; j<=m; ++j)
				if(s[i][j]=='0') b[i][j]=b[i-1][j]+a[i][j]-1;
		for(int i=3; i<=n; ++i)
			for(int j=1; j<=m; ++j)
				if(s[i-1][j]=='0'&&s[i][j]=='0')
					ansc=(ansc+1ll*b[i-2][j]*(a[i][j]-1))%p,
					f[i][j]=1ll*b[i-2][j]*(a[i][j]-1)%p;
		for(int i=1,sum=0; i<=m; ++i,sum=0)
			for(int j=4; j<=n; ++j)
				if(s[j][i]=='0')
					sum=(sum+f[j-1][i])%p,
					ansf=(ansf+sum)%p;
				else sum=0;
		printf("%d %d\n",ansc*mulc,ansf*mulf);
	}
    return 0;
}