#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e3+10,mod=998244353;
int T,id,n,m,c,f,a[N][N],r[N][N],d[N][N],s[N][N],sf[N][N];
ll ans1,ans2;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),id=read();
	while(T--)
	{
		ans1=ans2=0;
		memset(s,0,sizeof(s));
		memset(sf,0,sizeof(sf));
		n=read(),m=read(),c=read(),f=read();
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				char s;
				cin>>s;
				a[i][j]=s-'0';
				r[i][j]=j;
				d[i][j]=i;
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=m-1;j>=1;j--)
			{
				if(!a[i][j]&&!a[i][j+1])
					r[i][j]=r[i][j+1];
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=n-1;j>=1;j--)
			{
				if(!a[j][i]&&!a[j+1][i])
					d[j][i]=d[j+1][i];
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=n;j>=1;j--)
			{
				s[j][i]=s[j+1][i];
				int wid=r[j][i]-i+1;
				if(wid>1)
				{
					s[j][i]=(s[j][i]+wid-1)%mod;
					sf[j][i]=((sf[j][i]+(wid-1)*(d[j][i]-j)%mod)%mod+mod)%mod;
				}	
				if(d[j][i]-j>2&&wid>1)
				{
					ans2=(ans2+(sf[j+2][i]-sf[d[j][i]][i])*(wid-1)%mod)%mod;
				}
				if(d[j][i]-j>1&&wid>1)
				{
					ans1=(ans1+(s[j+2][i]-s[d[j][i]+1][i])*(wid-1)%mod)%mod;
				}
			}
		}
		ans1=(ans1+mod)%mod;
		ans2=(ans2+mod)%mod;
		cout<<c*ans1%mod<<" "<<f*ans2%mod<<endl;
	}
	return 0;
}
