#include<bits/stdc++.h>
using namespace std;
namespace __Plozia__
{
typedef long long ll;
typedef pair<int,int> pii;
constexpr int Max=1010;
constexpr ll mod=998244353;
inline void mod_add(ll &x,const ll &y)
{
	x+=y;
	if(x>=mod)
		x-=mod;
}
bool a[Max][Max];
int f[Max][Max];//right
int g[Max][Max];//down
ll p[Max],q[Max];
int n,m,id;
ll C,F;
void solve()
{
	cin>>n>>m>>C>>F;
	char ch;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			cin>>ch,a[i][j]=ch-'0';
	for(int k=1;k<=n;++k)
		for(int i=m;i;--i)
			if(a[k][i])
				f[k][i]=0;
			else
				f[k][i]=f[k][i+1]+1;
	for(int k=1;k<=m;++k)
		for(int i=n;i;--i)
			if(a[i][k])
				g[i][k]=0;
			else
				g[i][k]=g[i+1][k]+1;
	ll s1=0,s2=0;
	for(int k=1;k<=m;++k)
	{
		memset(p,0,sizeof(p));
		memset(q,0,sizeof(q));
		for(int i=n;i;--i)
			if(!a[i][k])
			{
				p[i]=f[i][k]-1;
				q[i]=(ll)(f[i][k]-1)*(g[i][k]-1);
			}
		ll u=0,v=0;
		for(int i=n;i;--i)
		{
			if(i+2<=n&&!a[i+1][k]&&!a[i+2][k])
				mod_add(u,p[i+2]),mod_add(v,q[i+2]);
			if(!a[i][k])
			{
				mod_add(s1,u*(f[i][k]-1)%mod);
				mod_add(s2,v*(f[i][k]-1)%mod);
			}
			else
				u=0,v=0;
		}
	}
	cout<<C*s1%mod<<" "<<F*s2%mod<<"\n";
}
void clear()
{
	memset(a,false,sizeof(a));
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	memset(p,0,sizeof(p));
	memset(q,0,sizeof(q));
}
signed plozia_main()
{
	int t;
	cin>>t>>id;
	while(t--)
		solve(),clear();
	return 0;
}
}
signed main()
{
	freopen("plant.in","r",stdin),freopen("plant.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	__Plozia__::plozia_main();
	return 0;
}
