#include <bits/stdc++.h>
#define int long long
#define mod 998244353
#define OK
using namespace std;
int read ()
{
    char ch=getchar();
    int flag=1,x=0;
    while (ch<'0' || ch>'9')
    {
    	if (ch=='-') flag=-flag;
    	ch=getchar();
	}
	while (ch>='0' && ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return flag*x;
}
int T,id,n,m,c,f,ans;
char mp[1005][1005];
int rit[1005][1005],down[1005][1005];
int pre[1005][1005],ppre[1005][1005];
signed main ()
{
#ifdef OK
	freopen ("plant.in","r",stdin);
	freopen ("plant.out","w",stdout);
#endif
	T=read(); id=read();
	while (T--)
	{
		n=read(); m=read(); c=read(); f=read();
		for (int i=1;i<=n;i++)
			scanf ("%s",mp[i]+1);
		for (int i=1;i<=n;i++)
			rit[i][m+1]=-1;
		for (int i=1;i<=m;i++)
			down[n+1][i]=-1;
		for (int i=n;i>=1;i--)
			for (int j=m;j>=1;j--)
				if (mp[i][j]=='1')
					rit[i][j]=down[i][j]=-1;
				else
				{
					rit[i][j]=rit[i][j+1]+1;
					down[i][j]=down[i+1][j]+1;
				}
		for (int i=1;i<=m;i++)
		{
			pre[0][i]=0;
			for (int j=1;j<=n;j++)
				pre[j][i]=(pre[j-1][i]+rit[j][i]+mod)%mod;
		}
		for (int i=1;i<=m;i++)
		{
			ppre[0][i]=0;
			for (int j=1;j<=n;j++)
				ppre[j][i]=(ppre[j-1][i]+pre[j][i]+mod)%mod;
		}
//		for (int i=1;i<=n;i++)
//		{
//			for (int j=1;j<=m;j++)
//				printf ("%d ",ppre[i][j]<10000?ppre[i][j]:ppre[i][j]-mod);
//			putchar (10);
//		}
		if (c)
		{
			ans=0;
			for (int i=1;i<=n;i++)
				for (int j=1;j<=m;j++)
					if (rit[i][j]>0 && down[i][j]>1)
						ans=(ans+rit[i][j]*(pre[i+down[i][j]][j]-pre[i+1][j]+mod)%mod)%mod;
			printf ("%lld ",ans);
		}
		else printf ("0 ");
		if (f)
		{
			ans=0;
			for (int i=1;i<=n;i++)
				for (int j=1;j<=m;j++)
					if (rit[i][j]>0 && down[i][j]>2)
						ans=(ans+rit[i][j]*(
							(ppre[i+down[i][j]-1][j]-ppre[i+1][j]-(down[i][j]-2)*pre[i+1][j]%mod)
							%mod+mod)%mod)%mod;
			printf ("%lld\n",ans);
		}
		else puts ("0");
	}
#ifndef OK
	printf ("%d\n",clock());
#endif
	return 0;
}
// 每次想按 Win 总是按到 Fn，烦死了 
// 此题输出 "0 0" 能拿 1 分
// 样例 3 "114 514"... 
