#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+10,mod=998244353;
int T,id;
int n,m;
ll c,f;
int a[N][N],cnth[N][N],cnts[N][N],cnt3[N][N],cnt5[N][N];
string s[N];
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--)
	{
		ll totc=0,totf=0;
		scanf("%d%d%lld%lld",&n,&m,&c,&f);
		for(int i=0;i<=n+1;i++)
		{
			for(int j=0;j<=m+1;j++)
			{
				a[i][j]=1;
				cnth[i][j]=-1;
				cnts[j][i]=-1;
			}
		}
		for(int i=1;i<=n;i++)
		{
			cin>>s[i];
			for(int j=0;j<m;j++)
			{
				a[i][j+1]=s[i][j]-48;
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=m;j>=1;j--)
			{
				if(a[i][j]==1)
					cnth[i][j]=-1;
				else
					cnth[i][j]=cnth[i][j+1]+1;
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				cnt3[i][j]=cnt3[i][j-1]+cnth[j][i];
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=n;j>=1;j--)
			{
				if(a[j][i]==1)
					cnts[i][j]=-1;
				else
					cnts[i][j]=cnts[i][j+1]+1;
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				cnt5[i][j]=cnt5[i][j-1]+cnth[j][i]*cnts[i][j];
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(cnts[i][j]<=1||a[j][i]==1)
					continue;
				int y=j+cnts[i][j];
				totc=(totc+(cnt3[i][y]-cnt3[i][j+1])*(cnt3[i][j]-cnt3[i][j-1])%mod)%mod;
				if(cnts[i][j]<=2)
					continue;
				totf=(totf+(cnt5[i][y-1]-cnt5[i][j+1])*(cnt3[i][j]-cnt3[i][j-1])%mod)%mod;
			}
		}
		printf("%lld %lld\n",totc*c%mod,totf*f%mod);
	}
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/