#include<bits/stdc++.h>
#define gc getchar()
using namespace std;
template <class T>
inline void read(T &x)
{
	x=0;
	int f=1;
	char ch=gc;
	while (!isdigit(ch))
	{
		if (ch=='-') f=-1;
		ch=gc;
	}
	while (isdigit(ch))
	{
		x=x*10+ch-'0';
		ch=gc;
	}
	x*=f;
	return;
}
bool a[1100][1100];//vis[1100][1100];
long long m,n;
const long long mod=998244353;
long long ansc,ansf;
int cpx=1,cpy=0;
inline bool fda(int stx,int sty)//shu x heng y
{
	int fl=false;
	for (int i=stx;i<=n;i++)
	{
		for (int j=sty;j<=m;j++)
		{
			if (fl)
			{
				fl=false;
				j=1;
			}
			if ((!a[i][j])&&((i!=stx)||(j!=sty)))
			{
				cpx=i,cpy=j;
				return true;
			}
		}
		fl=true;
	}
	return false;
}
void wrk()
{
	//int fdx,fdy;
	int upxl=0,downxl=0,yl=0;
	while (fda(cpx,cpy))
	{
		//cout<<"cpx:"<<cpx<<' '<<"cpy:"<<cpy<<'\n';
		upxl=0,downxl=0,yl=0;
		//fdx=cpx,fdy=cpy;
		if (!a[cpx+1][cpy])
		{
			for (int i=cpy;i<=m;i++)
			{
				if (!a[cpx][i]) upxl++;
				else break;
			}
			//cout<<"upxl:"<<upxl<<'\n';
			for (int i=cpx;i<=n;i++)
			{
				if (!a[i][cpy]) yl++;
				else break;
			}
			//cout<<"yl:"<<yl<<'\n';
			for (int i=cpx+2;i<=n;i++)
			{
				if (!a[i][cpy])
				{
					downxl=0;
					for (int j=cpy;j<=m;j++)
					{
						if (!a[i][j]) downxl++;
						else break;
					}
					ansc+=(((downxl-1)*(upxl-1))%mod);
					ansf+=(((downxl-1)*(upxl-1)*(yl-i+cpx-1))%mod);
					if (ansc>=mod) ansc%=mod;
					if (ansf>=mod) ansf%=mod;
				}
				else break;
			}
		}
		else continue;
	}
	return;
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id,x,c,f;
	read(T);
	read(id);
	while (T--)
	{
		ansc=0,ansf=0;
		a[1][0]=true;
		read(n),read(m),read(c),read(f);
		for (int i=1;i<=n;i++)
		{
			for (int j=1;j<=m;j++)
			{
				x=getchar();
				if (x=='0') a[i][j]=false;
				else if (x=='1') a[i][j]=true;
				else j--;
				//vis[i][j]=false;
			}
		}
		if ((c==0)&&(f==0))
		{
			cout<<0<<' '<<0<<endl;
			continue;
		}
		else wrk();
		cout<<((c*ansc)%mod)<<' '<<((f*ansf)%mod)<<endl;
	}
	return 0;
}
