#include<bits/stdc++.h>
#define For(i,x,y) for (int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (int i=(x);i<(y);i++)
#define Dow(i,x,y) for (int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define fil(a,b) memset(a,b,sizeof a)
#define all(x) x.begin(),x.end()
#define siz(x) ((int)x.size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
	ll x=0,f=1;char c=getchar();
	while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
	if (c=='-') f=-1,c=getchar();
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

const int N = 1010, mod = 998244353;
int n,m,c,f,r[N][N],d[N][N];
char s[N][N];
inline void solve(){
	n=read(),m=read(),c=read(),f=read();
	For(i,1,n) scanf("%s",s[i]+1);
	For(i,1,n){
		r[i][m+1]=0;
		Dow(j,m,1) r[i][j]=(s[i][j]=='0'?r[i][j+1]+1:0);
	}
	For(j,1,m){
		d[n+1][j]=0;
		Dow(i,n,1) d[i][j]=(s[i][j]=='0'?d[i+1][j]+1:0);
	}
	int ans1=0,ans2=0;
	For(j,1,m){
		For(i,1,n) if (s[i][j]=='0'&&(i==1||s[i-1][j]=='1')){
			int k=i,s1=0;
			while (k<n&&s[k+1][j]=='0'){
				++k;
				if (k-2>=i) s1+=r[k-2][j]-1;
				ans1=(ans1+1ll*s1*(r[k][j]-1))%mod;
				ans2=(ans2+1ll*s1*(r[k][j]-1)%mod*(d[k][j]-1))%mod;
			}
		}
	}
	ans1*=c,ans2*=f;
	printf("%d %d\n",ans1,ans2);
}

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(),id=read();
	while (T--) solve();
}
