#include<bits/stdc++.h>
#define inf (1<<30)
#define INF (1ll<<60)
#define pii pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define siz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define clr(a) memset((a),0,sizeof(a))
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define pt(a) cerr<<#a<<'='<<a<<' '
#define pts(a) cerr<<#a<<'='<<a<<'\n'
#define int long long
using namespace std;
const int MAXN=1010;
const int MOD=998244353;
int a[MAXN][MAXN],suf[MAXN][MAXN];
int col[MAXN][MAXN],dn[MAXN][MAXN];
void solve(){
	int n,m,c,f;
	cin>>n>>m>>c>>f;
	rep(i,1,n) rep(j,1,m){
		char ch;cin>>ch;
		a[i][j]=ch-'0';
	}
	rep(i,1,n) per(j,m,1){
		if(a[i][j]) suf[i][j]=0;
		else suf[i][j]=suf[i][j+1]+1;
	}
	per(i,n,1) rep(j,1,m){
		if(a[i][j]) col[i][j]=0;
		else col[i][j]=(col[i+1][j]+suf[i][j]+MOD-1)%MOD;
	}
	rep(j,1,m){
		int cur=0;
		per(i,n,1){
			if(a[i][j]) cur=0,dn[i][j]=0;
			else{
				cur++;
				dn[i][j]=(dn[i+1][j]+(suf[i][j]+MOD-1)*(cur+MOD-1)%MOD)%MOD;
			}
		}
	}
	int ansc=0,ansf=0;
	rep(i,1,n-2) rep(j,1,m-1){
		if(a[i+1][j]||a[i][j]) continue;
		(ansc+=(suf[i][j]+MOD-1)*col[i+2][j]%MOD)%=MOD;
		(ansf+=(suf[i][j]+MOD-1)*dn[i+2][j]%MOD)%=MOD;
	}
	cout<<ansc*c<<' '<<ansf*f<<'\n';
	clr(a);clr(suf);clr(col);clr(dn);
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T,id;for(cin>>T>>id;T--;)
		solve();
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000
*/