#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
string st;
ll t,n,m,id,G[1010][1010],c,f;
int kdx[1010][1010],kdy[1010][1010];
ll mod=998244353,ans=0;
ll ansC,ansF;
inline ll read(){
	ll x=0;
	bool f=true;
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar())
		if(ch=='-')f=false;
	for(;isdigit(ch);ch=getchar())
		x=(x<<1)+(x<<3)+ch-'0';
	return f?x:~(x-1);
}
int searx(int x,int y){
	for(int j=y;j<=m;j++){
		if(kdx[x][j]==0)return kdx[x][j-1];
	}
	return kdx[x][m];
}
int seary(int x,int y){
	for(int i=x;i<=n;i++){
		if(kdy[i][y]==0)return kdy[i-1][y];
	}
	return kdy[n][y];
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	t=read(),id=read();
	while(t--){
		ansC=0,ansF=0;
		memset(G,0,sizeof(G));
		memset(kdx,0,sizeof(kdx));
		memset(kdy,0,sizeof(kdy));
		n=read();
		m=read();
		c=read(),f=read();
		for(int i=1;i<=n;i++){
			cin>>st;
			for(int j=0;j<m;j++){
				G[i][j+1]=st[j]-48;
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(G[i][j]==1)
					kdx[i][j]=0;
				else if(G[i][j]==0){
						kdx[i][j]=1;
						if(G[i][j-1]==0)
							kdx[i][j]=kdx[i][j-1]+1;
				}
			}
		}
		for(int j=1;j<=m;j++){
			for(int i=1;i<=n;i++){
				if(G[i][j]==1)
					kdy[i][j]=0;
				else if(G[i][j]==0){
					kdy[i][j]=1;
					if(G[i-1][j]==0)kdy[i][j]=kdy[i-1][j]+1;
				}
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				int xx=0,xxx=0;
				if(kdx[i][j])xx=searx(i,j)-kdx[i][j];
				for(int k=i+1;k<=n;k++){
					if(G[k][j]==1||G[i+1][j]==1)break;
					if(G[k][j]==0&&k!=i+1){
						ansC=(ansC+(searx(k,j)-kdx[k][j])*xx%mod)%mod;
						ansF=(ansF+(searx(k,j)-kdx[k][j])*xx*(seary(k,j)-kdy[k][j])%mod)%mod;
					}
				}
			}
		}
		printf("%lld %lld\n",ansC*c%mod,ansF*f%mod);
	}
	return 0;
}