#include<bits/stdc++.h>
using namespace std;
int read()
{
	char ch;
	while((ch=getchar())<'0'||ch>'9');
	int res=ch-48;
	while((ch=getchar())>='0'&&ch<='9')
		res=(res<<3)+(res<<1)+ch-48;
	return res;
}
long long int T,n,m,c,f,id,mod;
long long int h[1105][1105],p[1105][1105],pre[1105][1105],sum,ansc,ansf;
string s[1005];
void work()
{
	for(long long int j=0;j<m-1;++j)
	{
		sum=0;
		for(long long int i=2;i<n;++i)
		{
			if(s[i][j]=='1'||s[i-1][j]=='1'||s[i-2][j]=='1') sum=0;
			else
			{
				if(pre[i][j]==-1) continue;
				p[i][j]=p[pre[i][j]][j]/h[pre[i][j]][j]*h[i][j];
				if(pre[i][j]!=i-1) p[i][j]+=h[pre[i][j]][j]*h[i][j];
				if(pre[i][j]>0&&h[pre[i][j]-1][j]>0) p[i][j]+=h[pre[i][j]-1][j]*h[i][j]; 	
				ansc+=p[i][j];ansc%=mod;
				if(i+1<n&&s[i+1][j]=='0') ansf+=sum+p[i][j],ansf%=mod;
				sum+=p[i][j];
			}
		}
	}
}
int main()
{
	freopen("plant.in","r",stdin);freopen("plant.out","w",stdout);
	cin>>T>>id;mod=998244353;
	while(T--)
	{
		cin>>n>>m>>c>>f;
		for(long long int i=0;i<n;++i) cin>>s[i];
		ansc=ansf=0;memset(pre,-1,sizeof(pre));
		memset(h,-1,sizeof(h));memset(p,0,sizeof(p));
		for(long long int i=0;i<n;++i)
			for(long long int j=m-1;j>-1;--j)
				if(s[i][j]=='0') 
				{
					h[i][j]=h[i][j+1]+1;
					if(!i) continue;
					if(h[i-1][j]>0) pre[i][j]=i-1;
					else if(!h[i-1][j]) pre[i][j]=pre[i-1][j];
				}	
		work();
		if(c) cout<<ansc<<' ';else cout<<0<<' ';
		if(f) cout<<ansf<<'\n';else cout<<0<<'\n';
	}
	return 0;
}
