#include<bits/stdc++.h>
#define rint register int
#define ll long long
#define mod 998244353
#define N 1005
using namespace std;
int T,id;
int n,m,ccc,f;
int a[N][N],b[N][N],c[N][N];
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	if (id==1)
	{
		for (rint i=1;i<=T;i++)
			printf("%d %d\n",0,0);
		return 0;
	}
	while (T--)
	{
		ll sumc=0;
		ll sumf=0;
		scanf("%d%d%d%d",&n,&m,&ccc,&f);
		string s;
		for (rint i=1;i<N;i++)
			for (rint j=1;j<N;j++)
			{
				b[i][j]=0;
				c[i][j]=0;
				a[i][j]=0;
			}
		for (rint i=1;i<=n;i++)
		{
			cin>>s;
			for (rint j=1;j<=m;j++)
			{
				a[i][j]=s[j-1]-48;
			}
		}
		for (rint i=1;i<=n;i++)
		{
			for (rint j=m;j>=1;j--)
			{
				if (a[i][j]==1) b[i][j]=0;
				else b[i][j]=b[i][j+1]+1;
			}
		}
		for (rint j=1;j<=m;j++)
		{
			for (rint i=n;i>=1;i--)
			{
				if (a[i][j]==1) c[i][j]=0;
				else c[i][j]=c[i+1][j]+1;
			}
		}
//		for (rint i=1;i<=n;i++)
//		{
//			for (rint j=1;j<=m;j++)
//			cout<<a[i][j]<<" ";
//			cout<<endl;
//		}
//		for (rint i=1;i<=n;i++)
//		{
//			for (rint j=1;j<=m;j++)
//			cout<<c[i][j]<<" ";
//			cout<<endl;
//		}
		for (rint i=1;i<=n;i++)
		{
			for (rint j=1;j<=m;j++)
			if (b[i][j]>1&&c[i][j]>=3)
			{
				for (rint k=i+2;k<=i+c[i][j]-1;k++)
				if (b[k][j]>1)
				{
					sumc=(sumc+(ll)(ccc)*(1ll)*(b[i][j]-1)*(b[k][j]-1))%mod;
					sumf=(sumf+(ll)(f)*(1ll)*(c[k][j]-1)*(b[k][j]-1)*(b[i][j]-1))%mod;
					//cout<<i<<" "<<j<<"    "<<k<<" "<<j<<endl;
				}
			}
		}
		printf("%lld %lld\n",sumc%mod,sumf%mod);
	}
	fclose(stdin);fclose(stdout);
	return 0;
}