#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e3+10;
const int mod=998244353;
int n,m,c,f,a[N][N];
int lft[N][N],upd[N][N],dnd[N][N];
int ansc,ansf;
char s[N][N];
int read(){
	char ch=getchar();
	int x=0;
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(),id=read();
	while(T--){
		//cout<<"Ilovecsy\n";
		n=read(),m=read(),c=read(),f=read();
		ansc=0,ansf=0;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)cin>>s[i][j];
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)a[i][j]=s[i][j]-'0';
		for(int i=1;i<=n;i++){
			lft[i][m+1]=0;
			for(int j=m;j>=1;j--){
				if(!a[i][j])lft[i][j]=lft[i][j+1]+1;
				else lft[i][j]=0;
			}
		}
		for(int j=1;j<=m;j++){
			dnd[n+1][j]=0;
			for(int i=n;i>=1;i--){
				if(!a[i][j])dnd[i][j]=dnd[i+1][j]+1;
				else dnd[i][j]=0;
			}
		}
		for(int j=1;j<=m;j++){
			upd[0][j]=0;
			for(int i=1;i<=n;i++){
				if(!lft[i-1][j])upd[i][j]=0;
				else upd[i][j]=(upd[i-1][j]+lft[i-1][j]-1)%mod;
	//			cout<<i<<' '<<j<<' '<<upd[i][j]<<endl;
			}
		}
		for(int j=1;j<=m;j++){
			for(int i=2;i<=n;i++){
				if(lft[i][j]&&lft[i-1][j])ansc=(ansc+upd[i-1][j]*(lft[i][j]-1)%mod)%mod;
				if(lft[i][j]&&lft[i-1][j]&&dnd[i][j])ansf=(ansf+upd[i-1][j]*(lft[i][j]-1)%mod*(dnd[i][j]-1)%mod)%mod;
			}
		}
		cout<<ansc*c%mod<<' '<<ansf*f%mod<<endl;
	}
	return 0;
}