#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
const int o=1e3+10;
char s[o][o];
int T,id,n,m,c,f,a[o][o],b[o][o];
signed main(){
  freopen("plant.in","r",stdin);
  freopen("plant.out","w",stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin>>T>>id;
  while (T--){
  	int ans1=0,ans2=0;
  	cin>>n>>m>>c>>f;
  	memset(a,0,sizeof(a));
  	memset(b,0,sizeof(b));
  	for (int i=1;i<=n;i++)
  		cin>>(s[i]+1);
  	for (int i=1;i<=n;i++)
  		for (int j=m-1;j>=1;j--)
  			if (s[i][j+1]=='0'&&s[i][j]=='0') a[i][j]=a[i][j+1]+1;
  	for (int i=1;i<=m;i++)
  		for (int j=n-1;j>=1;j--)
  			if (s[j+1][i]=='0'&&s[j][i]=='0') b[j][i]=b[j+1][i]+1;
  	for (int i=1;i<=m;i++){
  		int q=0;
  		for (int j=n-1;j>=1;j--)
  			if (s[j][i]=='0') ans1=(ans1+q*a[j][i]%mod)%mod,q=(q+a[j+1][i])%mod;
  									 else q=0;
  	}
  	for (int i=1;i<=m;i++){
  		int q=0;
			for (int j=n-1;j>=1;j--)
				if (s[j][i]=='0') ans2=(ans2+q*a[j][i]%mod)%mod,q=(q+a[j+1][i]*b[j+1][i]%mod)%mod;
				   					 else q=0;
  	}
  	cout<<ans1*c<<" "<<ans2*f<<"\n";
	}
  return 0;
}
