#include<bits/stdc++.h>
using namespace std;

constexpr int maxn=1010;
constexpr long long mod=998244353;

char a[maxn][maxn];
int r[maxn][maxn],d[maxn][maxn];

int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	scanf("%d%d",&T,&id);
	while(T--)
	{
		memset(r,0,sizeof r);
		memset(d,0,sizeof d);
		int i,j,n,m,c,f;
		long long vc=0,vf=0;
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(i=1;i<=n;i++)
		{
			scanf("%s",a[i]+1);
			for(j=m;j>=1;j--)
			{
				if(a[i][j]=='1') r[i][j]=0;
				else r[i][j]=r[i][j+1]+1;
			}
		}
		for(j=1;j<=m;j++)
		{
			for(i=n;i>=1;i--)
			{
				if(a[i][j]=='1') d[i][j]=0;
				else d[i][j]=d[i+1][j]+1;
			}
			long long sum=0;
			for(i=3;i<=n;i++)
			{
				if(a[i][j]=='0'&&a[i-1][j]=='0'&&a[i-2][j]=='0')
				{
					sum=(sum+r[i-2][j]-1)%mod;
					vc=(vc+sum*(r[i][j]-1))%mod;
					vf=(vf+sum*(r[i][j]-1)*(d[i][j]-1))%mod;
				}
				else sum=0;
			}
		}
		printf("%lld %lld\n",vc*c,vf*f);
	}
	return 0;
}