#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int n, m;
char map[105][105];
bool vis[105][105][2][2][2][2];
struct node
{
node(int p,int q,bool a1,bool a2,bool a3,bool a4,int a5)
{
x=p, y=q, b=a1, ye=a2, r=a3, g=a4, now=a5;
}
node() {}
int x, y, now;
bool b, ye, r, g;
};
bool can(int x,int y)
{
if(x<0||y<0||x>=n||y>=m)
return false;
if(map[x][y]=='#')
return false;
return true;
}
int dx[]= {1,0,-1,0}, dy[]= {0,1,0,-1};
int main()
{
int i, j, sx, sy;
bool flag;
while(~scanf("%d%d",&n,&m),n&&m)
{
queue<node> q;
flag=false;
for(i=0; i<n; i++)
{
scanf("%s",map[i]);
for(j=0; j<m; j++)
{
if(map[i][j]=='*')
sx=i, sy=j;
if(map[i][j]=='X')
flag=true;
}
}
if(!flag)
{
puts("The poor student is trapped!");
continue;
}
memset(vis,0,sizeof(vis));
vis[sx][sy][0][0][0][0]=true;
q.push(node(sx,sy,false,false,false,false,0));
node tmp;
map[sx][sy]='.';
flag=false;
int x, y, nx, ny, now, cc;
bool b, ye, r, g;
while(!q.empty())
{
tmp=q.front();
q.pop();
x=tmp.x, y=tmp.y, b=tmp.b, ye=tmp.ye, r=tmp.r, g=tmp.g, now=tmp.now;
bool vb, vye, vr, vg;
if(map[x][y]=='X')
{
flag=true;
cc=now;
break;
}
for(i=0; i<4; i++)
{
nx=x+dx[i], ny=y+dy[i];
if(!can(nx,ny)) continue;
if(map[nx][ny]=='.'||map[nx][ny]=='X')
{
if(!vis[nx][ny][b][ye][r][g])
q.push(node(nx,ny,b,ye,r,g,now+1));
vis[nx][ny][b][ye][r][g]=true;
}
else if(map[nx][ny]>='a'&&map[nx][ny]<='z')
{
vb=b, vye=ye, vr=r, vg=g;
if(map[nx][ny]=='b')
vb=true;
if(map[nx][ny]=='y')
vye=true;
if(map[nx][ny]=='r')
vr=true;
if(map[nx][ny]=='g')
vg=true;
if(!vis[nx][ny][b][ye][r][g])
q.push(node(nx,ny,vb,vye,vr,vg,now+1));
vis[nx][ny][b][ye][r][g]=true;
}
else if(map[nx][ny]>='A'&&map[nx][ny]<='Z')
{
bool ans=false;
char kk=map[nx][ny];
if(kk=='B'&&b)
ans=true;
if(kk=='Y'&&ye)
ans=true;
if(kk=='R'&&r)
ans=true;
if(kk=='G'&&g)
ans=true;
if(ans)
{
if(!vis[nx][ny][b][ye][r][g])
q.push(node(nx,ny,b,ye,r,g,now+1));
vis[nx][ny][b][ye][r][g]=true;
}
}
}
}
if(flag) printf("Escape possible in %d steps.\n",cc);
else puts("The poor student is trapped!");
}
return 0;
}