#include<bits/stdc++.h>
using namespace std;
const int P=998244353;
const int maxn=1005;
int tu[maxn][maxn];
int sumx[maxn][maxn],sumy[maxn][maxn];
char s[maxn][maxn];
int n,m,c,f;
int T,id;
long long Vc,Vf;
bool flag[maxn][maxn];
void solvc(int x,int y){
	for(int i=x+2;i<=x+sumy[x][y]-1;i++){
		if(sumx[i][y]>=2)
			Vc=(Vc+(sumx[x][y]-1)*(sumx[i][y]-1))%P;
	}
}
void solvf(int x,int y){
	for(int i=x+2;i<=x+sumy[x][y]-1;i++){
		if(sumx[i][y]>=2&&flag[i+1][y])
			Vf=(Vf+(sumx[x][y]-1)*(sumx[i][y]-1)*(sumy[i][y]-1))%P;
	}
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0);
	cin>>T>>id;
	if(id!=1){
		while(T--){
			Vc=0,Vf=0;
			memset(sumx,0,sizeof sumx);
			memset(sumy,0,sizeof sumy);
			cin>>n>>m>>c>>f;
			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++){
					tu[i][j]=1;
					if(s[i][j]=='0')tu[i][j]=0;
				}
			for(int i=n;i>=1;i--)
				for(int j=m;j>=1;j--){
					if(tu[i][j]==0){
						flag[i][j]=1;
						sumx[i][j]=sumx[i][j+1]+1;
						sumy[i][j]=sumy[i+1][j]+1;
					}
				}
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++)
					if(flag&&sumx[i][j]>=2){
						solvc(i,j);
						solvf(i,j);
					}
			cout<<Vc*c%P<<' '<<Vf*f%P;
		}
	}
	else if(id==1){
		while(T--){
			cin>>n>>m>>c>>f;
			int a;
			for(int i=1;i>=n;i++){
				for(int j=1;j<=m;j++){
					cin>>a;
				}
			}
			cout<<c<<" "<<f;
		}
	}
	return 0;
}
