#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int Mod=998244353;
const int maxn=1e3+5;
int G[maxn][maxn];
int F[maxn][maxn];
//F[x][l]表示第x行从l开始最长有多少0 
int T,id,n,m,c,f;
ll ans1,ans2;
char ch;

void add(ll &a,ll b){
	a=a+b;if(a>=Mod) a-=Mod; 
}

void init(int x){
	int l=1,r=1;
	while(l<=r && r<=m){
		while(!G[x][r] && r<=m) {
			r++;
		}
		while(l<r){
			F[x][l]=r-l;
			l++;
		}
		if(G[x][r]) F[x][r]=0;
		l++;r++;
	}
	return;
}

void solvec(){
	ans1=0;
	ans2=0;
	for(int j=1;j<m;j++){
		int l=1,r=1;
		while(l<=r && r<=n){
			while(F[l][j]<2) {
				l++;r++;
			}
			int k=F[l][j]-1; 
			while(!G[r][j] && r<=n){
				r++;
			}
			if(r-l<=2){
				l=++r;continue;
			}
			r--;
			for(int t=l+2;t<=r;t++){
				//printf("%d %d!!\n",ans1,ans2);
				if(F[t][j]-1<1) continue;
				add(ans1,k*(F[t][j]-1)*1ll); 
				if(r-t<1) continue;
				add(ans2,k*(F[t][j]-1)*1ll*(r-t));
			}
			l++;
		}
	}
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	//scanf("%d%d",&T,&id);
	cin>>T>>id;
	while(T--){
		memset(G,0,sizeof(G));
		cin>>n>>m>>c>>f;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>ch;
				G[i][j]=(ch-'0');
			}
			init(i);
		}
		solvec();
		ans1=(ans1*c*1LL+Mod)%Mod; 
		ans2=(ans2*f*1LL+Mod)%Mod; 
		printf("%lld %lld\n",ans1,ans2);
		//cout<<ans1*1ll<<" "<<ans2<<endl;
	}
	return 0;
}
