/*
after dusk passed,
there is a starry sky.
*/
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
#define mod 998244353
#define int long long
#define len(a) (int)a.size()
using namespace std;
const int N=1100;
int T,n,m,c,f,to[N][N],tmp[N],w;
char s[N][N];
inline void add(int &a,int b){a=((a+b>=mod)?a+b-mod:a+b);}
inline void del(int &a,int b){a=((a-b<0)?a-b+mod:a-b);}
inline void mul(int &a,int b){a=a*b%mod;}
inline int m_pow(int a,int b)
{
	int ans=1;
	while (b)
	{
		if (b&1) mul(ans,a);
		b>>=1;
		mul(a,a);
	}
	return ans;
}
inline int read()
{
	int f=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
int solve_c(int a[],int n)
{
	int sum=0,ans=0;
	for (int i=n-1;i>=1;i--)
	{
		add(ans,sum*a[i]%mod);
		add(sum,a[i+1]);
	}
	return ans;
}
int solve_f(int a[],int n)
{
	int sum=0,ans=0;
	for (int i=n-1;i>=1;i--)
	{
		add(ans,sum*a[i]%mod);
		add(sum,(n-i-1)*a[i+1]%mod);
	}
	return ans;
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read();read();
	while (T--)
	{
		n=read();m=read();c=read();f=read();
		for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
		for (int i=1;i<=n;i++)
		{
			for (int j=1;j<=m;)
			{
				int k=j;
				while (k<=m&&s[i][j]==s[i][k]) k++;
				if (s[i][j]=='0') fill(to[i]+j,to[i]+k,k-1);
				j=k;
			}
		}
		int vc=0,vf=0;
		for (int j=1;j<=m;j++)
		{
			for (int i=1;i<=n;)
			{
				int k=i;
				while (k<=n&&s[i][j]==s[k][j]) k++;
				if (s[i][j]=='0')
				{
					w=0;
					for (int p=i;p<k;p++) tmp[++w]=to[p][j]-j;
					add(vc,solve_c(tmp,w));
					add(vf,solve_f(tmp,w));
				}
				i=k;
			}
		}
		mul(vc,c);mul(vf,f);
		printf("%lld %lld\n",vc,vf);
	}
}