#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#define _rep(i,a,b) for(int i=(a);i<=(b);i++)
#define _per(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
typedef long long ll;
template<typename Tp>
inline void Rd(Tp &x){
	int f=1;char ch=getchar();
	x=0;
	while(ch<'0' || ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch && ch<='9') x=x*10+ch-'0', ch=getchar();
}
template<typename Tp>
inline void Rc(Tp &x){
	char ch=getchar();
	while(ch!='0' && ch!='1') ch=getchar();
	x=ch-'0';
}
const int N=1010,mod=998244353;
int n,m,c,f;
bool mp[N][N];//1表示可以.
ll fy[N][N],fx[N][N],g[N][N],h[N][N];
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,useless;Rd(T),Rd(useless);
	while(T--){
		Rd(n),Rd(m),Rd(c),Rd(f);
		memset(fy,0,sizeof(fy));
		memset(fx,0,sizeof(fx));
		memset(g,0,sizeof(g));
		memset(h,0,sizeof(h));
		_rep(i,1,n)
			_rep(j,1,m)
				Rc(mp[i][j]),mp[i][j]^=1;
		_per(i,n,1)
			_per(j,m,1){
				fy[i][j]=(j==m||mp[i][j+1]==0||mp[i][j]==0? 0:1+fy[i][j+1])%mod;
				fx[i][j]=(i==n||mp[i+1][j]==0||mp[i][j]==0? 0:1+fx[i+1][j])%mod;
				g[i][j]=(mp[i][j]==0? 0:fy[i][j]+(i<n? g[i+1][j]:0))%mod;//若=0,记得重置.
				h[i][j]=(mp[i][j]==0? 0:fy[i][j]*fx[i][j]+(i<n? h[i+1][j]:0))%mod;
			}
		ll C=0,F=0;
		_rep(i,1,n-1)
			_rep(j,1,m){
				if(i<n)
					C=(C+fy[i][j]*(g[i+1][j]-fy[i+1][j]))%mod;
				if(i<n-1)
					F=(F+fy[i][j]*(h[i+1][j]-fy[i+1][j]*fx[i+1][j]))%mod;
			}
		printf("%d %d\n",(C*c%mod+mod)%mod, (F*f%mod+mod)%mod);
	}
	return 0;
}
