// Zoj 1649 # include <stdio. h> # include <queue> # include <string. h> using namespace STD; # define maxn 200 # define INF 1000000 struct point {int X, Y; int step, time ;}; queue <point> q; int n, m, ax, ay; char map [maxn] [maxn]; int time [maxn] [maxn]; int dir [4] [2] ={{-1, 0 }, {}, {0,-1}, {}; int BFS (point S) {int I; q. push (s); point JK; while (! Q. empty () {JK = Q. front (); q. pop (); for (I = 0; I <4; I ++) {int x = JK. X + dir [I] [0]; int y = JK. Y + dir [I] [1]; if (x> = 0 & x <= N-1 & Y> = 0 & Y <= M-1 & map [x] [Y]! = '#') {Point t; T. X = x; T. y = y; T. step = JK. step + 1; T. time = JK. time + 1; if (Map [x] [Y] = 'X') T. time ++; If (T. time <time [x] [Y]) {time [x] [Y] = T. time; q. push (t) ;}}} return time [ax] [AY];} int main () {int I, j; // freopen ("input.txt ", "r", stdin); While (scanf ("% d", & N, & M )! = EOF) {memset (MAP, 0, sizeof (MAP); for (I = 0; I <n; I ++) scanf ("% s ", map [I]); int Sx, Sy; point start; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {time [I] [J] = inf; If (Map [I] [J] = 'A') {ax = I; ay = J ;} else if (Map [I] [J] = 'R') {SX = I; Sy = J ;}} start. X = SX; start. y = sy; start. step = 0; start. time = 0; int min = BFS (start); If (Min <inf) printf ("% d \ n", min ); else printf ("Poor Angel has to stay in the prison all his life. \ n ") ;}return 0 ;}