#include<iostream>
#include<cstring>
#include<cstdio>
#define int long long
using namespace std;
const int N=1005,mod=998244353;

int read(){
	char c=getchar();int f=1,x=0;
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=x*10+c-48;c=getchar();}
	return x*f;
}

int t,id,n,m,c,f,ans1,ans2,ans3,sum1,sum2,a[N][N],rig[N][N],dow[N][N];
char ch[N];
//bool vis[N][N];

void work(int x,int y){
	for(int i=x+2;i<=x+dow[x][y]-1;++i){
		if(rig[i][y]<=1)continue;
		ans1=(ans1+rig[i][y]-1)%mod;
		if(a[i+1][y]==1||i+1>n)continue;
//		ans3+=rig[i][y]-1;
//ans3=max(ans3,ans1);
		ans3=(ans3+rig[i][y]-1)%mod;
		ans2=(ans2+dow[i+1][y])%mod;
	}
//cout<<x<<" "<<y<<" "<<ans1<<" "<<ans2<<" "<<ans3<<endl;	
//	cout<<ans2<<endl;
	ans1=ans1*(rig[x][y]-1)%mod;
	ans2=(ans2*ans3%mod*(rig[x][y]-1)%mod);
}

//void work(int i,int j){
//	if(a[i][j+1]==1)return ;
//	if(a[i+1][j]==1||a[i+2][j]==1)return ;
//	int dow=0,rig1=0,rig2=0;
//	for(int qwq=j;qwq<=n;qwq++){
//		if(a[i][qwq]==0)rig1++;
//	}
//	for(int qwq=i;qwq<=m;++qwq){
//		if(a[])
//	}
//}

signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	t=read(),id=read();
	if(id==1){
		while(t--){puts("0 0");}
		return 0;
	}
	while(t--){
		n=read(),m=read(),c=read(),f=read();
		for(int i=1;i<=n;++i){
			scanf("%s",ch+1);
			for(int j=1;j<=m;++j){
				if(ch[j]=='1')a[i][j]=1;
				else rig[i][j]=1,dow[i][j]=1;
			}
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=m;j>=1;--j){
			if(a[i][j]==1){
//				cout<<0<<" ";
				continue;
			}
			rig[i][j]=rig[i][j+1]+rig[i][j];
//			cout<<rig[i][j]<<" ";
		}
//		puts("");
	}
	for(int i=1;i<=m;++i){
		for(int j=n;j>=1;--j){
			if(a[j][i]==1)continue;
			dow[j][i]=dow[j+1][i]+dow[j][i];
		}
	}
//	for(int i=1;i<=n;++i){
//		for(int j=1;j<=m;++j){
//			cout<<dow[i][j]<<" ";
//		}
//		puts("");
//	}
	for(int i=1;i<=n-2;++i){
		for(int j=1;j<=m-1;++j){
//			cout<<i<<j<<endl;
			if(a[i][j]==1||rig[i][j]==1||dow[i][j]<3)continue;
			ans1=0,ans2=0,ans3=0;
			work(i,j);
//			cout<<i<<j<<endl;

			sum1=(sum1+ans1)%mod,sum2=(sum2+ans2)%mod;
		}
	}
	printf("%lld %lld",sum1*c,sum2*f);
	return 0;
}
