#define NDEBUG

#include <cassert>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

#ifndef NDEBUG
#define PV(var) std::cerr<<#var<<": "<<var<<"; ";
#define PN std::cerr<<std::endl;
#else
#define PV(...)
#define PN
#endif

char lab[120][120];
int got[120][120][16];

struct Pos
{
	int x, y, k, d;
	Pos(int x, int y, int k, int d)
		:x(x), y(y), k(k), d(d)
	{
	}
};

#define BK (1<<0)
#define YK (1<<1)
#define RK (1<<2)
#define GK (1<<3)

int main2()
{
	int R, C;
	queue<Pos> qu;
	scanf("%d %d", &R, &C);

	if(R==0 && C==0) return 1;
	for(int j=0; j<=C+1; ++j) lab[0][j]=lab[R+1][j]='#';
	for(int i=1; i<=R; ++i)
	{
		lab[i][0]=lab[i][C+1]='#';
		scanf("%*[^#.*BYRGbyrgX]");
		for(int j=1; j<=C; ++j)
		{
			for(int k=0; k<16; ++k)	got[i][j][k]=0;
			scanf("%c", &lab[i][j]);
			if(lab[i][j]=='*') qu.push(Pos(j, i, 0, 0));
		}
		lab[i][C+2]=0;
	}
	for(int i=0; i<=R+1; ++i)
	{
		PV(lab[i]);
		PN;
	}
	int result=-1;
	while(!qu.empty() && result<0)
	{
		Pos p=qu.front();
		PV(p.x);
		PV(p.y);
		PV(p.k);
		PN;
		qu.pop();
		for(int aa=0; aa<4; ++aa)
		{
			int dx, dy;
			switch(aa)
			{
				case 0: dx=-1; dy=0; break;
				case 1: dx=1; dy=0; break;
				case 2: dx=0; dy=-1; break;
				case 3: dx=0; dy=1; break;
			}
			bool add=false;
			int k=p.k;
			int i=p.y;
			int j=p.x;
			switch(lab[i+dy][j+dx])
			{
				case '#': break;
				case '.': add=true; break;
				case '*': add=true; break;
				case 'b': k|=BK; add=true; break;
				case 'y': k|=YK; add=true; break;
				case 'r': k|=RK; add=true; break;
				case 'g': k|=GK; add=true; break;
				case 'B': add=(k&BK); break;
				case 'R': add=(k&RK); break;
				case 'G': add=(k&GK); break;
				case 'Y': add=(k&YK); break;
				case 'X': result=p.d; break;
			}
			PV(lab[i+dy][j+dx])
				PV(add);
			PN;
			if(add && got[i+dy][j+dx][k]==0) qu.push(Pos(j+dx, i+dy, k, p.d+1));
			got[i+dy][j+dx][k]=1;
		}
	}
	if(result<0) printf("The poor student is trapped!\n");
	else printf("Escape possible in %d steps.\n", result+1);
	//return 1 if end of input
	return 0;
}

int main()
{
	int d=1000000000;
//	scanf("%d",&d);
	while(d-- && !main2());
	return 0;
}
