#include<bits/stdc++.h>
using namespace std;
namespace solve1{
	typedef long long ll;
	ll const N=1010,MOD=998244353;
	ll t,id,n,m,c,f,r[N][N],s[N][N],w[N][N];
	char a[N][N];
	void solve100pts(){
		cin>>t>>id;
		while(t--){
			memset(r,-1,sizeof r);
			memset(s,0,sizeof s);
			memset(w,0,sizeof w);
			cin>>n>>m>>c>>f;
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++)
					cin>>a[i][j],a[i][j]^=48;
			for(int i=1;i<=n;i++)
				for(int j=m;j;j--)
					if(!a[i][j])
						r[i][j]=r[i][j+1]+1;
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++)
					if(!a[i][j])
						s[i][j]=(r[i][j]+s[i-1][j])%MOD;
			for(int i=2;i<=n;i++)
				for(int j=1;j<=m;j++)
					if(!a[i][j]&&!a[i-1][j])
						w[i][j]=(s[i-2][j]*r[i][j]%MOD+w[i-1][j])%MOD;
			ll ansc=0,ansf=0;
			for(int i=2;i<=n;i++)
				for(int j=1;j<=m;j++)
					if(!a[i][j]){
						if(!a[i-1][j])
							ansc=(ansc+s[i-2][j]*r[i][j])%MOD;
						ansf=(ansf+w[i-1][j])%MOD;
					}
			cout<<(ansc*c%MOD+MOD)%MOD<<" "<<(ansf*f%MOD+MOD)%MOD<<"\n";
		}
	}
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0);
	solve1::solve100pts();
}
