#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1005,p=998244353;

ll n,m,i,j,c,f,id,t,resc,resf;
ll F[N][N],a[N][N],first[N][N],high[N][N],disi[N][N],dis[N][N],down[N][N];
char C;

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	scanf("%d%d",&t,&id);
	while(t--){
		scanf("%d%d%d%d",&n,&m,&c,&f);getchar();
		for(i=1;i<=n;++i){
			for(j=1;j<=m;++j){
				C=getchar();
				if(C=='\n'){--j;continue;}
				a[i][j]=C-'0';
			}
			
		}
		for(i=n;i>0;--i){
			int cur=m+1;
			for(j=m;j>0;--j){
				if(a[i][j]) cur=j;
				first[i][j]=cur;
				dis[i][j]=cur-j-1;
				if(dis[i][j]<0) dis[i][j]=0;
				down[i][j]=down[i+1][j]+dis[i][j];
				if(a[i][j]) down[i][j]=0;
			}
		}
		for(j=1;j<=m;++j){
			int cur=n+1;
			for(i=n;i>0;--i){
				if(a[i][j]) cur=i;
				high[i][j]=cur;
				disi[i][j]=cur-i-1;
				if(disi[i][j]<0) disi[i][j]=0;
				F[i][j]=F[i+1][j]+disi[i][j]*dis[i][j];
				if(a[i][j]) F[i][j]=0;
			}
		}
		
		for(i=1;i<=n;++i){
			for(j=1;j<=m;++j){
				if(a[i+1][j]) continue;
				resc+=dis[i][j]*down[i+2][j];
				resc%=p;
				resf+=dis[i][j]*F[i+2][j];
				resf%=p;
			}
		}
		cout<<resc*c<<" "<<resf*f<<endl;
	}
	return 0;
}