#include<bits/stdc++.h>
using namespace std;
int n,m,c,f;
int mp[1010][1010]={0};
int mpn[1010][1010]={0},mpm[1010][1010]={0};
int ans=0,sum=0;
void make(int t,string str)
{
	for(int i=0;i<m;i++)
	{
		mp[t][i+1]=str[i]-'0';
	}
	return;
}
void qz()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			mpn[i][j]=mp[i][j];
			mpm[i][j]=mp[i][j];
			mpn[i][j]+=mpn[i-1][j];
			mpm[i][j]+=mpm[i][j-1];
		}
	}
	return;
}
void find()
{
	for(int i=1;i<m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			for(int k=j+2;k<=n;k++)
			{
				if(mpn[k][i]==mpn[j-1][i])
				{
					//cout<<"@@"<<i<<" "<<j<<" "<<k<<endl;
					int tmp1=i+1,tmp2=i+1;
					if(mpm[j][tmp1]!=mpm[j][i-1])continue;
					if(mpm[k][tmp2]!=mpm[k][i-1])continue;
					while(mpm[j][tmp1]==mpm[j][i-1]&&tmp1<=m)tmp1++;
					while(mpm[k][tmp2]==mpm[k][i-1]&&tmp2<=m)tmp2++;
					long long s=(tmp1-1-i)*(tmp2-1-i);
					ans+=s;
					ans%=998244353;
					if(f==1)
					{
						int tmp3=k+1;
						if(mpn[tmp3][i]!=mpn[k-1][i])continue;
						while(mpn[tmp3][i]==mpn[k-1][i]&&tmp3<=n)tmp3++;
						sum+=s*(tmp3-1-k);
						sum%=998244353;
					}	
				}
				else
				{
					break;
				}
			}
		}
	}
	return;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int t,id;
	cin>>t>>id;
	while(t--)
	{
		cin>>n>>m>>c>>f;
		ans=0;
		sum=0;
		for(int i=1;i<=n;i++)
		{
			string str;
			cin>>str;
			make(i,str);
		}
		qz();
		/*for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				printf("%d ",mpn[i][j]);
			}
			printf("\n");
		}*/
		if(c==0&&f==0)
		{
			printf("0 0\n");
		}
		else if(c==0)
		{
			find();
			printf("0 %d\n",sum);
		}
		else if(f==0)
		{
			find();
			printf("%d 0\n",ans);
		}
		else
		{
			find();
			printf("%d %d\n",ans,sum);
		}
	}
	return 0;
}