#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mo=998244353;
struct jj
{
	int line,begin,end;
};
queue<jj> q1,q2;
int a[1001][1001],b[1001][1001],sum1[1001][1001],n,m,c,f;
void Pre()
{
	memset(b,0,sizeof(b));
	memset(sum1,0,sizeof(sum1));
	for (int i=1;i<=m;i++)
		a[n+1][i]=1;
	for (int j=1;j<=m;j++)
	{
		int sum=0,fi=1;
		for (int i=1;i<=n+1;i++)
		{
			if (a[i][j]==0)
			{
				if (sum==0)fi=i;
				sum++;
			}
			else 
			{
				if (sum>=3)
				{
					q1.push((jj){j,fi,i-1});
				}
				sum=0;
			}
			
		}
	}
	for (int j=1;j<=m;j++)
	{
		int sum=0,fi=1;
		for (int i=1;i<=n+1;i++)
		{
			if (a[i][j]==0)
			{
				if (sum==0)fi=i;
				sum++;
			}
			else 
			{
				if (sum>=4)
				{
					q2.push((jj){j,fi,i-1});
				}
				sum=0;
			}
		}
	}
	for (int i=1;i<=n;i++)
	{
		int sum=0;
		for (int j=m;j>=1;j--)
		{
			if (a[i][j]==0)
			{
				sum++;
				b[i][j]=sum;
			}
			else sum=0;
		}
	}
	for (int j=1;j<=m;j++)
	{
		int sum=0;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j]==0)
			{
				sum1[i][j]=sum1[i-1][j]+b[i][j]-1;
			}
		}
	}
	return;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	scanf("%d%d",&T,&id);
	while (T--)
	{
		while (!q1.empty())q1.pop();
		while (!q2.empty())q2.pop();
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for (int i=1;i<=n;i++)
		{
			char s[1010];
			cin>>s;
			for (int j=1;j<=m;j++)
				if (s[j-1]=='0')
					a[i][j]=0;
				else	
					a[i][j]=1;
		}   
		Pre();
		ll res=0;
		if (c)
		{
			while (!q1.empty())
			{
				int l=q1.front().line;
				int x=q1.front().begin;
				int y=q1.front().end;
				q1.pop();
				for (int i=x+2;i<=y;i++)
					{
						res+=1ll*(sum1[i-2][l])*(b[i][l]-1)%mo;
						res%=mo;
					}
			 }
		}
		printf("%lld ",c*res);
		res=0;
		if (f)
		{
			while (!q2.empty())
			{
				int l=q2.front().line;
				int x=q2.front().begin;
				int y=q2.front().end;
				q2.pop();
				for (int i=x+2;i<=y-1;i++)
				{
					res+=1ll*(sum1[i-2][l])*(b[i][l]-1)%mo*(y-i)%mo;
					res%=mo;
				}
			 }
		}
		printf("%lld",f*res);
		printf("\n");
	} 
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/