var tp:array [0..101,0..101] of char;
    t,t2,t0,t3:array [0..101,0..101,0..1,0..1,0..1,0..1] of boolean;
    i,j,k,l,m,n,o,p,q,blue,yellow,red,green,r,c:longint;
    bb:boolean;
    
procedure valami;
begin
    while true do begin
     bb:=true;
     for i:=0 to r+1 do
      for j:=0 to c+1 do
       for blue:=0 to 1 do
        for yellow:=0 to 1 do
         for red:=0 to 1 do
          for green:=0 to 1 do if t[i,j,blue,yellow,red,green]<>t2[i,j,blue,yellow,red,green] then bb:=false;
     if bb and (o>0) then begin writeln('The poor student is trapped!'); exit; end;
     inc(o);
//     t3:=t;
     t:=t2;
     t2:=t0;
     for i:=0 to r+1 do
      for j:=0 to c+1 do
       for blue:=0 to 1 do
        for yellow:=0 to 1 do
         for red:=0 to 1 do
          for green:=0 to 1 do if t[i,j,blue,yellow,red,green] then begin
           t2[i,j,blue,yellow,red,green]:=true;
           if tp[i-1,j]='.' then t2[i-1,j,blue,yellow,red,green]:=true;
           if tp[i+1,j]='.' then t2[i+1,j,blue,yellow,red,green]:=true;
           if tp[i,j-1]='.' then t2[i,j-1,blue,yellow,red,green]:=true;
           if tp[i,j+1]='.' then t2[i,j+1,blue,yellow,red,green]:=true;
           if tp[i-1,j]='X' then begin writeln('Escape possible in ',o,' steps.'); exit; end;
           if tp[i+1,j]='X' then begin writeln('Escape possible in ',o,' steps.'); exit; end;
           if tp[i,j-1]='X' then begin writeln('Escape possible in ',o,' steps.'); exit; end;
           if tp[i,j+1]='X' then begin writeln('Escape possible in ',o,' steps.'); exit; end;
           if tp[i-1,j]='b' then t2[i-1,j,1,yellow,red,green]:=true;
           if tp[i+1,j]='b' then t2[i+1,j,1,yellow,red,green]:=true;
           if tp[i,j-1]='b' then t2[i,j-1,1,yellow,red,green]:=true;
           if tp[i,j+1]='b' then t2[i,j+1,1,yellow,red,green]:=true;
           if tp[i-1,j]='y' then t2[i-1,j,blue,1,red,green]:=true;
           if tp[i+1,j]='y' then t2[i+1,j,blue,1,red,green]:=true;
           if tp[i,j-1]='y' then t2[i,j-1,blue,1,red,green]:=true;
           if tp[i,j+1]='y' then t2[i,j+1,blue,1,red,green]:=true;
           if tp[i-1,j]='r' then t2[i-1,j,blue,yellow,1,green]:=true;
           if tp[i+1,j]='r' then t2[i+1,j,blue,yellow,1,green]:=true;
           if tp[i,j-1]='r' then t2[i,j-1,blue,yellow,1,green]:=true;
           if tp[i,j+1]='r' then t2[i,j+1,blue,yellow,1,green]:=true;
           if tp[i-1,j]='g' then t2[i-1,j,blue,yellow,red,1]:=true;
           if tp[i+1,j]='g' then t2[i+1,j,blue,yellow,red,1]:=true;
           if tp[i,j-1]='g' then t2[i,j-1,blue,yellow,red,1]:=true;
           if tp[i,j+1]='g' then t2[i,j+1,blue,yellow,red,1]:=true;
           if blue=1 then begin
            if tp[i-1,j]='B' then t2[i-1,j,blue,yellow,red,green]:=true;
            if tp[i+1,j]='B' then t2[i+1,j,blue,yellow,red,green]:=true;
            if tp[i,j-1]='B' then t2[i,j-1,blue,yellow,red,green]:=true;
            if tp[i,j+1]='B' then t2[i,j+1,blue,yellow,red,green]:=true;
           end;
           if yellow=1 then begin
            if tp[i-1,j]='Y' then t2[i-1,j,blue,yellow,red,green]:=true;
            if tp[i+1,j]='Y' then t2[i+1,j,blue,yellow,red,green]:=true;
            if tp[i,j-1]='Y' then t2[i,j-1,blue,yellow,red,green]:=true;
            if tp[i,j+1]='Y' then t2[i,j+1,blue,yellow,red,green]:=true;
           end;
           if red=1 then begin
            if tp[i-1,j]='R' then t2[i-1,j,blue,yellow,red,green]:=true;
            if tp[i+1,j]='R' then t2[i+1,j,blue,yellow,red,green]:=true;
            if tp[i,j-1]='R' then t2[i,j-1,blue,yellow,red,green]:=true;
            if tp[i,j+1]='R' then t2[i,j+1,blue,yellow,red,green]:=true;
           end;
           if green=1 then begin
            if tp[i-1,j]='G' then t2[i-1,j,blue,yellow,red,green]:=true;
            if tp[i+1,j]='G' then t2[i+1,j,blue,yellow,red,green]:=true;
            if tp[i,j-1]='G' then t2[i,j-1,blue,yellow,red,green]:=true;
            if tp[i,j+1]='G' then t2[i,j+1,blue,yellow,red,green]:=true;
           end;
          end;
    end;
end;
    
begin
   while true do begin
    readln(r,c);
    if r=0 then break;
    for i:=0 to r+1 do begin
     tp[i,0]:='#';
     tp[i,c+1]:='#';
    end;
    for i:=0 to c+1 do begin
     tp[0,i]:='#';
     tp[r+1,i]:='#';
    end;
    for i:=0 to 101 do
     for j:=0 to 101 do
      for blue:=0 to 1 do
       for yellow:=0 to 1 do
        for red:=0 to 1 do
         for green:=0 to 1 do 
           t0[i,j,blue,yellow,red,green]:=false;
    for i:=1 to r do begin
     for j:=1 to c do begin
      read(tp[i,j]);
      if tp[i,j]='*' then begin
       p:=i;
       q:=j;
       tp[i,j]:='.';
      end;
     end; 
     readln;
    end;
    readln;
    t2:=t0;
    t:=t0;
    t2[p,q,0,0,0,0]:=true;
    o:=0;
    valami;
   end;
end.