#include<bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
using namespace std;
template<typename T_>void operator+=(vector<T_>&x,const T_&y){x.push_back(y);}
const int N=1007,p=998244353;
int T,id,n,m,c,f,h[N][N],w[N][N];
string a[N];
int work1(){
	int res=0;
	for(int j=1;j<=m;j++){
		int t=0;
		for(int i=1;i<=n;i++){
			if(h[i][j]){
				(res+=1ll*t*(h[i][j]-1)%p)%=p;
				if(i>1)(t+=(h[i-1][j]<1)?0:(h[i-1][j]-1))%=p;
			}
			else t=0;
		}
	}
	return res;
}
int work2(){
	int res=0;
	for(int j=1;j<=m;j++){
		int t=0;
		for(int i=1;i<=n;i++){
			if(h[i][j]){
				(res+=1ll*t*(h[i][j]-1)*(w[i][j]-1)%p)%=p;
				if(i>1)(t+=(h[i-1][j]<1)?0:(h[i-1][j]-1))%=p;
			}
			else t=0;
		}
	}
	return res;
}
void work(){
	cin>>n>>m>>c>>f;
	memset(w,0,sizeof(w)),memset(h,0,sizeof(h));
	for(int i=0;i<=n+1;i++)a[i].clear();
	a[0].resize(m+2),a[n+1].resize(m+2);
	for(int i=1;i<=n;i++)cin>>a[i],a[i]=' '+a[i]+' ';
	for(int i=1;i<=n;i++)
		for(int j=m;j;j--)
			if(a[i][j]=='0')h[i][j]=h[i][j+1]+1;
			else h[i][j]=0;
	for(int j=1;j<=m;j++)
		for(int i=n;i;i--)
			if(a[i][j]=='0')w[i][j]=w[i+1][j]+1;
			else w[i][j]=0;
	if(c)cout<<work1()<<' ';
	else cout<<0<<' ';
	if(f)cout<<work2();
	else cout<<0;
	cout<<'\n';
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T>>id;
	while(T--)work();
	return 0;
}
