#include<bits/stdc++.h>
using namespace std;
inline long long read()
{
	long long s=0,k=1;
	char r=getchar();
	while(!isdigit(r))
	{
		k=(r=='-')?-1:1;
		r=getchar();
	}
	while(isdigit(r))
	{
		s=s*10+(r-'0');
		r=getchar();
	}
	return s*k;
}
#define d read()
#define ll long long
#define Maxn 10010
#define Size 1010
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Mod 998244353
inline ll ksm(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=ans*a%Mod;
		a=a*a%Mod;
		b>>=1;
	}
	return ans;
}
inline ll inv(ll x){return ksm(x,Mod-2);}
bool r[Size][Size];
ll h[Size][Size],l[Size][Size];
ll Sum[Size],Sum2[Size];
ll inv2;
void solve()
{
	ll n=d,m=d,C=d,F=d;
	char ch;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			cin>>ch;
			r[i][j]=(ch=='0');
		}
	for(int i=0;i<n;i++)
		for(int j=m-1;j>=0;j--)
			h[i][j]=((r[i][j])?(h[i][j+1]+1):0);
	for(int j=0;j<m;j++)
	{
		l[n][j]=n;
		for(int i=n-1;i>=0;i--)
			l[i][j]=((r[i][j])?l[i+1][j]:i);
	}
	ll ans1=0;
	for(int i=0;i<m;i++)
	{
		ll sum=0,sum2=0,las=0;
		ll query=0;
		for(int j=0;j<n;j++)
		{
			if(!r[j][i])
			{
				ans1=(ans1+(sum*sum%Mod+Mod-sum2)*inv(2)%Mod)%Mod;
				sum=sum2=las=0;
				query=0;
				continue;
			}
			sum+=h[j][i]-1;
			sum%=Mod;
			sum2+=(h[j][i]-1)*(h[j][i]-1)%Mod;
			sum2+=las*(h[j][i]-1)%Mod*2%Mod;
			sum2%=Mod;
			las=h[j][i]-1;
		}
		if(sum&&sum2)
			ans1=(ans1+(sum*sum%Mod+Mod-sum2)*inv(2)%Mod)%Mod;
	}
	printf("%lld ",ans1*C%Mod);
	ll ans2=0;
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
			Sum[j]=Sum2[j]=0;
		for(int j=0;j<n;j++)
		{
			if(!r[j][i]){j++;continue;}
			if(j==0) Sum[j]=h[j][i]-1,Sum[j]%=Mod;
			else Sum[j]=Sum[j-1]+h[j][i]-1,Sum[j]%=Mod;
			if(j==0) Sum[j]=j*(h[j][i]-1)%Mod;
			else Sum2[j]=Sum2[j-1]+j*(h[j][i]-1)%Mod,Sum2[j]%=Mod;
		}
		for(int j=0;j<n;j=l[j][i]+1)
		{
			if(h[j][i]<2) continue;
			ll sum=(Sum[l[j][i]-1]+Mod-Sum[j+1])%Mod;
			ll sum2=(Sum2[l[j][i]-1]+Mod-Sum2[j+1])%Mod;
			ans2+=(h[j][i]-1)*(sum*(l[j][i]-1)%Mod+Mod-sum2)%Mod;
			ans2%=Mod;
		}
	}
	printf("%lld\n",ans2*F%Mod);
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ll T=d;d;
	inv2=inv(2);
	while(T--) solve();
}
