#include<bits/stdc++.h>
#define int long long
#define Maxn (1005)

using namespace std;

inline int read()
{
	int pos=1,num=0;
	char ch=getchar();
	while (!(ch>='0'&&ch<='9'))
	{
		if (ch=='-') pos=-1;
		ch=getchar();
	}
	while (ch>='0'&&ch<='9')
	{
		num=num*10+((int)ch-'0');
		ch=getchar();
	}
	return pos*num;
}

inline void write(int x)
{
	if (x<0)
	{
		putchar('-');
		write(-x);
		return;
	}
	if (x>=10) write(x/10);
	putchar((char)(x%10)+'0');
	return;
}
inline void writeln(int x)
{
	write(x);
	puts("");
	return;
}
inline void writesp(int x)
{
	write(x);
	putchar(' ');
	return;
}
const int mod=998244353;

int hang[Maxn][Maxn],lie[Maxn][Maxn],lie1[Maxn][Maxn],F[Maxn][Maxn];

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(),id=read();
	while (T--)
	{
		int n=read(),m=read(),c=read(),f=read(),Vc=0,Vf=0;
		for (int i=1;i<=n;i++)for (int j=1;j<=m;j++)
		{
			char ch=getchar();
			while (!(ch=='0'||ch=='1')) ch=getchar();
			if (ch=='0') F[i][j]=1;else F[i][j]=0;
		}
		
		//hang
		memset(hang,0,sizeof(hang));
		for (int i=1;i<=n;i++)for (int j=m;j;j--)
			if (F[i][j]) hang[i][j]=hang[i][j+1]+1;else hang[i][j]=0;
		
		//lie
		if (c)
		{
			memset(lie,0,sizeof(lie));
			memset(lie1,0,sizeof(lie1));
			for (int j=1;j<=m;j++)for (int i=n;i;i--)
			{
//				writesp(i),writesp(j),writeln(hang[i][j]);
				if (F[i][j]) lie[i][j]=lie[i+1][j]+hang[i][j]-1;else lie[i][j]=0;
				if (F[i][j]) lie1[i][j]=lie1[i+1][j]+1;else lie1[i][j]=0;
//				if (lie1[i][j]>=3) writesp(i),writesp(j),writesp(hang[i][j]),writeln(lie[i+2][j]);
				if (lie1[i][j]>=3) (Vc+=(hang[i][j]-1)*(lie[i+2][j])%mod)%mod;
			}
		}
		writesp(Vc);
		if (f)
		{
			memset(lie,0,sizeof(lie));
			memset(lie1,0,sizeof(lie1));
			for (int j=1;j<=m;j++)for (int i=n;i;i--)
			{
				if (F[i][j]) lie[i][j]=lie[i+1][j]+(hang[i][j]-1)*lie1[i+1][j];else lie[i][j]=0;
				if (F[i][j]) lie1[i][j]=lie1[i+1][j]+1;else lie1[i][j]=0;
//				if (lie1[i][j]>=4) writesp(i),writesp(j),writesp(hang[i][j]),writeln(lie[i+2][j]);
				if (lie1[i][j]>=4) (Vf+=(hang[i][j]-1)*(lie[i+2][j])%mod)%mod;
			}
		}
		writeln(Vf);
	}
	return 0;
}

/*
1 0
4 3 1 1
001
010
000
000
*/