#include<bits/stdc++.h>
using namespace std;
namespace ye
{
#define int long long
#define sd(x) cout<<#x<<'='<<x<<endl
const int N=1e3+100,mod=998244353;	
int T,id,n,m,c,f,ansc,ansf;
char ori[N][N];
int r[N][N],d[N][N],sum[N];
void solve()
{
	ios::sync_with_stdio(false);
	cin>>T>>id;
	while(T--)
	{
		cin>>n>>m>>c>>f;
		for(int i=1;i<=n;i++)
		{
			cin>>(ori[i]+1);
		}
		memset(r,0,sizeof(r));
		memset(d,0,sizeof(d));
		for(int i=1;i<=n;i++)
			for(int j=m;j>=1;j--)
			if(ori[i][j]=='1')
				r[i][j]=0;
			else
				r[i][j]=r[i][j+1]+1;
		for(int i=1;i<=m;i++)
		{
			for(int j=n;j>=1;j--)
			if(ori[j][i]=='1')
				d[j][i]=0;
			else
				d[j][i]=d[j+1][i]+1;
		}
		//c
		ansc=0;
		for(int i=1;i<=m;i++)
		{
			memset(sum,0,sizeof(sum));
			for(int j=n;j>=1;j--)
			{
				if(ori[j][i]=='1')
					sum[j]=0;
				else
					sum[j]=(sum[j+1]+r[j][i]-1)%mod;
			}
			for(int j=1;j<=n-2;j++)
			{
				if(ori[j][i]=='1'||ori[j+1][i]=='1')
					continue;
				ansc=(ansc%mod+(r[j][i]-1)*sum[j+2]%mod)%mod;
			}
		}
		//f
		ansf=0;
		for(int i=1;i<=m;i++)
		{
			memset(sum,0,sizeof(sum));
			for(int j=n;j>=1;j--)
			{
				if(ori[j][i]=='1')
					sum[j]=0;
				else
					sum[j]=(sum[j+1]%mod+(r[j][i]-1)*(d[j][i]-1)%mod)%mod;
			}
			for(int j=1;j<=n-2;j++)
			{
				if(ori[j][i]=='1'||ori[j+1][i]=='1')
					continue;
				ansf=(ansf%mod+(r[j][i]-1)*sum[j+2]%mod)%mod;
			}
		}
		cout<<(ansc*c)%mod<<' '<<(ansf*f)%mod<<endl;
	}
	
}
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ye::solve();
	return 0;
}
