//A
#include<bits/stdc++.h>
using namespace std;
long long n,m,x,y,z,c,f;
int t,idd;
long long mod=998244353;
long long ansc,ansf;
long long a[1010][1010];
long long b[1010][1010];
long long fx[1010][1010],fy[1010][1010];
long long cheng(long long x1,long long x2,long long x3)
{
	return ((x1%mod)*(x2%mod)%mod)*(x3%mod)%mod;
}
int main()
{
	long long i,j,k;
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&t,&idd);
	string s;
	while(t--)
	{
		memset(fx,0,sizeof(fx));
		memset(fy,0,sizeof(fy));
		memset(b,0,sizeof(b));
		
		scanf("%lld%lld%lld%lld",&n,&m,&c,&f);
		for(i=1;i<=n;i++)
		{
			cin>>s;
			for(j=1;j<=m;j++)
			a[i][j]=s[j-1]-'0';
		}
		
		for(i=1;i<=n;i++)
		for(j=m;j>=1;j--)
		if(a[i][j]==0) fx[i][j]=fx[i][j+1]+1;
		else if(a[i][j]==1) fx[i][j]=0;
		
		for(j=1;j<=m;j++)
		for(i=n;i>=1;i--)
		if(a[i][j]==0) fy[i][j]=fy[i+1][j]+1;
		else if(a[i][j]==1) fy[i][j]=0;
		
		for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
		if(fy[i][j]>=3)
		{
			b[i][0]++;
			b[i][b[i][0]]=j;
		}
		
		ansc=0;
		for(i=1;i<=n;i++)
		for(j=1;j<=b[i][0];j++)
		for(k=i+2;k<=i+fy[i][b[i][j]]-1;k++)
		if(fx[i][b[i][j]]>=2&&fx[k][b[i][j]]>=2)
		ansc=(ansc+cheng(fx[i][b[i][j]]-1,fx[k][b[i][j]]-1,1))%mod;
			

		
		ansf=0;
		for(i=1;i<=n;i++)
		for(j=1;j<=b[i][0];j++)
		for(k=i+2;k<=i+fy[i][j]-1;k++)
		if(fx[i][j]>=2&&fx[k][j]>=2)
		ansf=(ansf+cheng(fx[i][j]-1,fx[k][j]-1,fy[k][j]-1))%mod;
		
		
		
		printf("%lld %lld\n",c*ansc,f*ansf);
		
	}
	
	return 0;
}