#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1005][1005],n,m,ans1,ans2,c,f,t,id,now,sum1,sum2,sum3,sum4;
char b[1005][1005];
const ll mod=998244353;
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
scanf("%lld%lld",&t,&id);
while(t--)
{
scanf("%lld%lld%lld%lld",&n,&m,&c,&f);
    if(c==0&f==0)
    {
    	cout<<0<<" "<<0<<endl;
    	return 0;
	}
	sum1=sum2=sum3=ans1=ans2=0;
	sum4=1;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			cin>>b[i][j];
			a[i][j]=b[i][j]-'0';
		}
	}
    for(ll j=1;j<=m;j++)
    {	
    	for(ll l=1;l<=n;l++)
    	{
    		if(a[l][j]==1)
    		continue;
		    sum1=0;	
		    now=l;
    		for(ll i=l;i<=n;i++)
    	    {
    		if(a[i][j]==0)
    		sum1++;
			else
			{
			 break;
			}
			   if(sum1>=3)
			  {
				for(ll k=j+1;k<=m;k++)
				{
				if(a[now][k]==0)
				sum2++;
				else
				break;	
				}
				for(ll k=j+1;k<=m;k++)
				{
				if(a[i][k]==0)
				sum3++;
				else
				break;
				}
				ans1=(ans1+sum2*sum3)%mod;
				sum2=sum3=0;
			  }
		    }
		}
	}
	if(f==1)
	{
	for(ll j=1;j<=m;j++)
       {
    	for(ll l=1;l<=n;l++)
    	{
    		if(a[l][j]==1)
    		continue;
    		now=l;
    		sum1=0;
    		for(ll i=l;i<=n;i++)
    		{
    			if(a[i][j]==0)
    			{
    				sum1++;
				}else
				{
				break;
				}
				if(sum1>=4)
				{
					for(ll k=j+1;k<=m;k++)
				   {
					if(a[now][k]==0)
				    sum2++;	
				    else
				    break;
				   }
				   for(ll k=j+1;k<=m;k++)
				   {
				   	if(a[i-1][k]==0)
					sum3++;
					else
					break;
				   }	   
				   for(ll k=i+1;k<=n;k++)
				   {
				   	if(a[k][j]==0)
				   	sum4++;
					else
					break;
				   }
				   ans2=(ans2+sum2*sum3*sum4)%mod;
				   sum2=sum3=0;
				   sum4=1;
				}
			}
		}
	  }
	}
	cout<<ans1*c%mod<<" "<<ans2*f%mod<<endl;
}
return 0;
}