#include<bits/stdc++.h>
#define For(i,l,r) for(int i=(l);i<=(r);++i)
#define ReFor(i,r,l) for(int i=(r);i>=(l);--i)
const int N=1010;
const int mod=998244353;
typedef long long ll;
using namespace std;
int T,id,n,m,c,f,top;
string s;
bool mp[N][N];
int R[N][N],D[N][N],sum1[N][N];
struct node{int num,x,y;}a[N];
void solve()
{
	scanf("%d%d%d%d",&n,&m,&c,&f);
	memset(R,0,sizeof(R));
	memset(D,0,sizeof(D));
	For(i,1,n)
	{
		cin>>s;
		For(j,1,m)
		{
			if(s[j-1]=='0')
				mp[i][j]=0;
			else
				mp[i][j]=1;
		}
	}
	For(i,1,n)
	{
		top=0;
		a[0].num=0;
		int j=m;
		while(j>=1)
		{
			if(j==1)
			{
				if(mp[i][j]==0)
				{
					++top;
					a[top].num=a[top-1].num+1;
					a[top].x=i;
					a[top].y=j;
					break;
				}
				else if(mp[i][j]==1)
					break;
			}
			else
			{
				if(mp[i][j]==1)
				{
					if(top!=0)
					{
						For(k,1,top)
							R[a[k].x][a[k].y]=a[k].num;
						top=0;
					}
					--j;
				}
				else if(mp[i][j]==0)
				{
					++top;
					a[top].num=a[top-1].num+1;
					a[top].x=i;
					a[top].y=j;
					--j;
				}
			}
		}
		if(top!=0)
		{
			For(k,1,top)
				R[a[k].x][a[k].y]=a[k].num;
			top=0;
		}
	}
	For(j,1,m)
	{
		top=0;
		a[0].num=0;
		int i=n;
		while(i>=1)
		{
			if(i==1)
			{
				if(mp[i][j]==0)
				{
					++top;
					a[top].num=a[top-1].num+1;
					a[top].x=i;
					a[top].y=j;
					break;
				}
				else if(mp[i][j]==1)
					break;
			}
			else
			{
				if(mp[i][j]==1)
				{
					if(top!=0)
					{
						For(k,1,top)
							D[a[k].x][a[k].y]=a[k].num;
						top=0;
					}
					--i;
				}
				else if(mp[i][j]==0)
				{
					++top;
					a[top].num=a[top-1].num+1;
					a[top].x=i;
					a[top].y=j;
					--i;
				}
			}
		}
		if(top!=0)
		{
			For(k,1,top)
				D[a[k].x][a[k].y]=a[k].num;
			top=0;
		}
	}
	ll sum_c=0,sum_f=0;
	memset(sum1,0,sizeof(sum1));
	ReFor(i,n,1)
	{
		For(j,1,m)
		{
			if(mp[i][j]==0)
				sum1[i][j]=((sum1[i+1][j]+(R[i][j]-1))%mod);
		}
	}
	For(i,1,n)
	{
		For(j,1,m)
		{
			if(mp[i][j]==1)
				continue;
			else if(D[i][j]>=3 && R[i][j]>=2)
				(sum_c+=(1ll*(sum1[i][j]-R[i][j]+2-R[i+1][j])*(R[i][j]-1)))%=mod;
		}
	}
	if(f==1)
	{
		For(i,1,n)
		{
			For(j,1,m)
			{
				if(mp[i][j]==1)
					continue;
				else if(D[i][j]>=4 && R[i][j]>=2)
				{
					ll tmp=(R[i][j]-1);
					For(k,2,D[i][j]-2)
					{
						if(R[i+k][j]==1)
							continue;
						(tmp*=(R[i+k][j]-1))%=mod;
						(tmp*=(D[i][j]-1-k))%=mod;
						(sum_f+=tmp)%=mod;
						tmp=(R[i][j]-1);
					}
				}	
			}
		}
	}
	(sum_c*=(1ll*c))%=mod;
	(sum_f*=(1ll*f))%=mod;
	printf("%lld %lld\n",sum_c,sum_f);
	return;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--)
		solve();
	return 0;
}