#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
const ll mo=998244353;
int T,id;
int n,m;
ll ansc,ansf,up,dw;
bool a[N][N];
ll r[N][N],cnt[N][N],sum,c,f;
char ch;
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while (T--)
	{
		scanf("%d%d%lld%lld",&n,&m,&c,&f);
		ansc=ansf=0;
		memset(a,0,sizeof(a));
		memset(r,0,sizeof(r));
		memset(cnt,0,sizeof(cnt));
		for (int i=1;i<=n;++i)
			for (int j=1;j<=m;++j)
			{
				ch=getchar();
				while (!(ch=='0'||ch=='1')) ch=getchar();
				a[i][j]=(ch=='1');
			}
		for (int i=1;i<=n;++i)
		{
			if (a[i][m]) r[i][m]=0;else r[i][m]=1LL;
			for (int j=m-1;j>=1;--j)
			if (a[i][j])r[i][j]=0;else r[i][j]=r[i][j+1]+1;
		}
		for (int j=1;j<=m;++j)
		{
			up=1LL,sum=0;
			for (int i=1;i<=n;++i)
			if (a[i][j]) up=(ll)i+1,sum=0;
			else {
				
				if (up+2<=i) sum=(sum+r[i-2][j]-1)%mo;
				cnt[i][j]=sum*(r[i][j]-1)%mo;
				ansc=(ansc+cnt[i][j])%mo;
			}
		}
		for (int j=1;j<=m;++j)
		{
			dw=(ll)n;
			for (int i=n;i>=1;--i)
			if (a[i][j]) dw=(ll)i-1;
			else{
				ansf=(ansf+cnt[i][j]*(ll)(dw-i)%mo)%mo;
			}
		}
		printf("%lld %lld\n",ansc*c,ansf*f);
	}
	return 0;
}
