#include<bits/stdc++.h>
//9:11

//9:45

//10:01
typedef long long ll;
const ll maxn=1010;
const ll mo=998244353;
using namespace std;
ll read(){
	ll res=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-f;ch=getchar();}
	while(isdigit(ch)) {res=res*10+(ch-'0');ch=getchar();}
	return res*f;
}
ll n,m,C,F;
char s[maxn];
bool a[maxn][maxn];
ll r[maxn][maxn];
ll d[maxn][maxn];
ll sum[maxn][maxn];
ll mul[maxn][maxn];
ll sum_mul[maxn][maxn];
ll cans,fans;
void solve(){
	n=read(),m=read(),C=read(),F=read();
	for(ll i=1;i<=n;i++) {
		scanf("%s",s+1);
		for(ll j=1;j<=m;j++){
			if(s[j]=='1')a[i][j]=1;
			else a[i][j]=0;
		}
	}
	for(ll i=1;i<=n;i++) a[i][m+1]=1;
	for(ll i=1;i<=m;i++) a[n+1][i]=1;
	
	for(ll i=1;i<=n;i++){
		for(ll j=m+1;j>=1;j--){
			if(a[i][j]==1){
				ll p=j-1;
				r[i][j]=j;
				while(a[i][p]==0 && p>=1){
					r[i][p]=j;
					p--;
				}j=p+1;
			}
		}
	}
	//right	//
	for(ll i=1;i<=m;i++){
		for(ll j=n+1;j>=1;j--){
			if(a[j][i]==1){
				ll p=j-1;
				d[j][i]=j;
				while(a[p][i]==0 && p>=1){
					d[p][i]=j;
					p--;
				}j=p+1;
			}
		}
	}
	//down
	for(ll j=1;j<=m;j++)for(ll i=1;i<=n;i++) sum[i][j]=sum[i-1][j]+r[i][j],sum[i][j]%=mo;
	
	for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++) mul[i][j]=(1ll*(r[i][j]-j-1)*(d[i][j]-i-1))%mo;
	
	for(ll j=1;j<=m;j++)for(ll i=1;i<=n;i++) sum_mul[i][j]=sum_mul[i-1][j]+mul[i][j],sum_mul[i][j]%=mo;
	
	//sum
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			if(d[i][j]-1 <= i+1) continue;
			//ll down=d[i][j]-1;
			//cout<<i<<" "<<j<<" "<<down<<"\n";
			cans=(cans+ 1ll*(sum[d[i][j]-1][j]-sum[i+1][j]-(d[i][j]-1-i-1)*(j+1) )*(r[i][j]-j-1)  )%mo;
		}
	}
	
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			if(d[i][j]-1 <= i+1) continue;
			//ll down=d[i][j]-1;
			//cout<<i<<" "<<j<<" "<<down<<"\n";
			fans=(fans+ 1ll*(sum_mul[d[i][j]-1-1][j]-sum_mul[i+1][j])*(r[i][j]-j-1)  )%mo;
		}
	}
			
	//cans=(cans+ (sum[d[i][j]-1][j]-sum[i+1][j]-(d[i][j]-1-i-1)*(j+1) )*1ll*(r[i][j]-j-1)  )%mo; clac c  n^2
	cout<<(cans*C)<<" "<<(fans*F)<<"\n";
	
	for(ll i=1;i<=n;i++) for(ll j=1;j<=m;j++) a[i][j]=0;
                    
	//for(ll i=1;i<=n;i++,cout<<"\n") for(ll j=1;j<=m;j++) cout<<d[i][j]<<" ";
	return;
}

signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ll T=read();
	ll id=read();
	while(T--){
		solve();
	}
	return 0;
}
/*

zai ci gan xie suo you zhi chi guo wo de ren.Everyone.

I will win full stars in my dream in the future.

Just keep going.

---------Sellaris
*/