#include<bits/stdc++.h>
using namespace std;
#define file(s) freopen(#s".in","r",stdin),freopen(#s".out","w",stdout)
#define ll long long
#define il inline
#define cst const
#define ret return
#define gc getchar
#define pc putchar
#define HgS 998244353
il ll rd()
{
	ll res=0;int lab=1;char ch=gc();
	while(ch<'0'||ch>'9')lab^=(ch=='-'),ch=gc();
	while((ch>='0')&&(ch<='9'))res=res*10+(ch&15),ch=gc();
	ret lab?res:-res;
}
il void prt(ll x,char tail='\n')
{
	static char s[45];int t=0;
	if(x<0)x=-x,pc('-');
	do{s[++t]=x%10,x/=10;}while(x);
	while(t)pc(s[t--]|48);
	if(tail)pc(tail);
}
ll T,id,n,m,c,f,nxt[1005][1005],sum[1005];char a[1005][1005];
int main()
{
	file(plant);
	T=rd();id=rd();
	while(T --> 0)
	{
		n=rd();m=rd();c=rd();f=rd();int rc=0,rf=0;
		for(int i=1;i<=n;++i)scanf("%s",a[i]+1);
		for(int i=1;i<=n;++i)
		{
			for(int j=m;j>=1;--j)
			{
				if(a[i][j]&1)nxt[i][j]=-1;
				else if(j==m||nxt[i][j+1]<j)nxt[i][j]=j;
				else nxt[i][j]=nxt[i][j+1];
			}
		}
		//for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)cerr<<nxt[i][j]<<" \n"[j==m];
		for(int j=1;j<=m;++j)
		{
			for(int i=1,l=1;i<=n+1;++i)
			{
				if(i>n||(a[i][j]&1))
				{
					sum[i]=0;
					for(int k=i-1;k>=l;--k)sum[k]=(sum[k+1]+nxt[k][j]-j)%HgS;
					if(c)for(int k=i-3;k>=l;--k)rc=(rc+1ll*(nxt[k][j]-j)*sum[k+2])%HgS;
					for(int k=i-1;k>=l;--k)sum[k]=(sum[k+1]+1ll*(nxt[k][j]-j)*(i-1-k))%HgS;
					if(f)for(int k=i-4;k>=l;--k)rf=(rf+1ll*(nxt[k][j]-j)*sum[k+2])%HgS;
					l=i+1;
				}
			}
		}
		prt(rc,' ');prt(rf);
	}
	ret 0;
}