#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char ch=getchar();int x=0;bool f=0;
	for(;!isdigit(ch);ch=getchar())if(ch=='-')f=1;
	for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
	if(f){x=-x;}return x;
}
#define int long long
const int mod=998244353;
const int maxn=1e3+5;
int n,m,s[maxn],a[maxn][maxn],T,id;
int b[maxn][maxn],ans1,ans2,C,F;
char c[maxn][maxn];
void solve(){
	n=read(),m=read();C=read(),F=read();
	ans1=0,ans2=0;
	memset(b,0,sizeof b);
	memset(a,0,sizeof a);
	for(int i=1;i<=n;i++){
		scanf("%s",c[i]+1);
		for(int j=1;j<=m;j++)a[i][j]=c[i][j]-'0',a[i][j]^=1;
	}
	for(int i=1;i<=n;i++){
		int o=0;
		for(int j=m;j>=1;j--){
			if(a[i][j]==0){o=0;}
			if(!o&&a[i][j]){o=j;}
			b[i][j]=o;
		}
	}
	for(int j=1;j<=m;j++){
		for(int i=1;i<=n;i++)if(a[i][j]){
			int l=i;
			while(a[l][j]==1&&l<=n){
				l++;
			}l--;s[i-1]=0;
			for(int k=i;k<=l;k++){
				s[k]=s[k-1]+b[k][j]-j;s[k]%=mod;
			}
			for(int k=i;k<l-1;k++)if(b[k][j]>j){
				ans1=ans1+(b[k][j]-j)*((s[l]-s[k+1]+mod)%mod)%mod;
				ans1%=mod;
			}
			s[i-1]=0;
			for(int k=i;k<=l;k++){
				s[k]=s[k-1]+(b[k][j]-j)*(l-k);s[k]%=mod;
			}
			for(int k=i;k<l-2;k++)if(b[k][j]>j){
				ans2=ans2+(b[k][j]-j)*((s[l]-s[k+1]+mod)%mod)%mod;
				ans2%=mod;
			}
			i=l;
		}
	}
	ans1=ans1*C%mod,ans2=ans2*F%mod;
	cout<<ans1<<" "<<ans2<<endl;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),id=read();
	while(T--)solve();
	return 0;
}
