#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read(void)
{int _;cin>>_;return _;}

const int mod=998244353;
const int N=1e3+5;
const int inf=0x3f3f3f3f;

int t,id,n,m,c,f,sum,lst,ans1,ans2,a[N][N];
char mp[N][N];
vector<int> v;

inline void calc1(void)
{
	if(v.empty()) return;
	int sum=0,n=(int)v.size();
	for(int i=1;i<n;i++) 
		sum=(sum+v[i])%mod;
	for(int i=1;i<n;i++)
	{
		sum=(sum-v[i]+mod)%mod;
		ans1=(ans1+sum*v[i-1]%mod)%mod;
	}
	ans1=(ans1%mod+mod)%mod;
	return;
}

inline void calc2(void)
{
	if(v.empty()) return;
	int sum=0,n=(int)v.size();
	for(int i=1;i<n;i++)
		sum=(sum+v[i]*(n-i-1)%mod)%mod;
	for(int i=1;i<n;i++)
	{
		sum=(sum-v[i]*(n-i-1)%mod)%mod;
		ans2=(ans2+sum*v[i-1]%mod)%mod;
	}
	ans2=(ans2%mod+mod)%mod;
	return;
}

inline void calc(void){
	calc1(),calc2(),v.clear();
}

inline void solve(void)
{
	n=read();m=read();
	c=read();f=read();
	lst=ans1=ans2=sum=0;
	for(int i=1;i<=n;i++)
		cin>>(mp[i]+1);
	for(int i=1;i<=n;i++)
	for(int j=m;j>=1;j--)
	{
		if(mp[i][j]=='1') lst=j-1;
		else lst=max(lst,j);
		a[i][j]=lst-j;
	}
	for(int j=1;j<=m;j++)
	{
		for(int i=1;i<=n;i++)
		{
			if(mp[i][j]=='1') calc();
			else v.push_back(a[i][j]);
		}
		calc();
	}
	cout<<(c*ans1)%mod<<" "<<(f*ans2)%mod<<endl;
	return;
}


signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	t=read();id=read();
	for(int i=1;i<=t;i++) solve();
	
	return 0;
}

/*

*/