#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353,N=1e3+110;
int read(){
	int x=0,f=1;char c=getchar();
	while(c>'9' || c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0' && c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return x*f;
}
int ansc,ansf,T,id,n,m,c,f,rt[N][N],dow[N][N],upf[N][N],dowf[N][N];
char mp[N][N];
void solve(){
	ansc=ansf=0;memset(rt,0,sizeof(rt));
				memset(dow,0,sizeof(dow));
				memset(upf,0,sizeof(upf));
				memset(dowf,0,sizeof(dowf));
	n=read(),m=read(),c=read(),f=read();
	for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
	for(int i=1;i<=n;i++)
		for(int j=m-1;j>=1;j--)
			if(mp[i][j]=='0' && mp[i][j+1]=='0')rt[i][j]=(rt[i][j+1]+1)%mod;
	for(int i=n-2;i>=1;i--)
		for(int j=1;j<=m;j++)
			if(mp[i][j]=='0' && mp[i+1][j]=='0' && mp[i+2][j]=='0')
			dow[i][j]=(dow[i+1][j]+rt[i+2][j])%mod;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			ansc+=rt[i][j]*dow[i][j],ansc%=mod;
	//-----------------
	for(int i=n-1;i>=1;i--)
		for(int j=1;j<=m;j++)
			if(mp[i][j]=='0' && mp[i+1][j]=='0')
				upf[i][j]=(upf[i+1][j]+1)%mod;
	for(int i=n-2;i>=1;i--)
		for(int j=1;j<=m;j++)
			if(mp[i][j]=='0' && mp[i+1][j]=='0' && mp[i+2][j]=='0')
			dowf[i][j]=(dowf[i+1][j]+rt[i+2][j]*upf[i+2][j]%mod)%mod;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			ansf+=rt[i][j]*dowf[i][j],ansf%=mod;
	
//	for(int i=1;i<=n;i++){
//		for(int j=1;j<=m;j++)
//			cout<<upf[i][j]<<" ";
//		cout<<endl;
//	}
	
	ansc*=c;
	ansf*=f;
	printf("%lld %lld",ansc,ansf);
	return;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),id=read();
	while(T--)solve();
	return 0;
}