#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define x first
#define y second
#define vi vector<int>
#define vpi vector<pii>
using namespace std;

inline void ckmin(int &a,int b){a=(a<b?a:b);}
inline void ckmax(int &a,int b){a=(a>b?a:b);}
inline int read()
{
	int f=1,num=0;char ch=getchar();
	while(!(ch=='-'||(ch<='9'&&ch>='0')))ch=getchar();
	if (ch=='-')f=-1,ch=getchar();
	while(ch<='9'&&ch>='0')num=num*10+ch-'0',ch=getchar();
	return num*f;
}

const int Mod=998244353;
const int M=1010;
int a[M][M],l[M][M],d[M][M],s[M][M],n,m,c,f;
char str[M];

void solve()
{
	n=read(),m=read(),c=read(),f=read();
	int S1=0,S2=0;
	for (int i=1;i<=n;i++)
	{
		scanf("%s",str);
		for (int j=0;j<m;j++)a[i][j+1]=(str[j]-'0');
	}
	for (int i=1;i<=n;i++)
	{
		if (a[i][m])l[i][m]=m;
		else l[i][m]=m+1;
		for (int j=m-1;j>=1;j--)
			if (a[i][j])l[i][j]=j;
			else l[i][j]=l[i][j+1];
	}
	for (int j=1;j<=m;j++)
	{
		if (a[n][j])d[n][j]=n;
		else d[n][j]=n+1;
		for (int i=n-1;i>=1;i--)
			if (a[i][j])d[i][j]=i;
			else d[i][j]=d[i+1][j];
	}
	for (int j=1;j<=m;j++)
	{
		int s1=0;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j]){s1=0,s[i][j]=0;continue;}
			s[i][j]=s1*(l[i][j]-j-1)%Mod;
			S1=(S1+s[i][j])%Mod,S2=(S2+s[i][j]*(d[i][j]-i-1))%Mod;
			if (i!=1&&!a[i-1][j])s1=(s1+l[i-1][j]-j-1)%Mod;
		}
	}
	cout<<(S1*c)%Mod<<' '<<(S2*f)%Mod<<'\n';
}

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read();read();
	while(T--)solve();
	return 0;
}
