#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace std;
int n,m,ans,tt;
char map[205][205];
int visit[205][205];
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
struct node
{
int x,y;
char w;
};
node noden[2005];
struct nodes
{
int x,y,t;
};
nodes node1,node2,pre[205][205];
//优先队列的建立,与一般队列不同的地方用蓝色标记
bool operator<(nodes a,nodes b)
{
return a.t>b.t;
}
int k=0,xx,yy;
void bfs()
{
priority_queue <nodes> q;
while(!q.empty())
q.pop();
node1.x=0;
node1.y=0;
node1.t=0;
visit[0][0]=1;
pre[0][0].x=0;
pre[0][0].y=0;
q.push(node1);
while(!q.empty())
{
node1=q.top();
q.pop();
if(node1.x==n-1&&node1.y==m-1)
{
ans=1;
tt=node1.t;
return ;
}
for(int i=0; i<4; i++)
{
node2.x=node1.x+dir[i][0];
node2.y=node1.y+dir[i][1];
if(node2.x>=0&&node2.x<n&&node2.y>=0&&node2.y<m&&map[node2.x][node2.y]!='X'&&!visit[node2.x][node2.y])
{
visit[node2.x][node2.y]=visit[node1.x][node1.y]+1;
if(map[node2.x][node2.y]=='.')
{
node2.t=node1.t+1;
}
else
{
int num=map[node2.x][node2.y]-'0';
node2.t=node1.t+1+num;
}
pre[node2.x][node2.y].x=node1.x;
pre[node2.x][node2.y].y=node1.y;
q.push(node2);
}
}
}
}
int t;
void print(int x,int y)
{
if(x==0&&y==0) return ;
print(pre[x][y].x,pre[x][y].y); //递归到第二个路径,回溯
printf("%ds:(%d,%d)->(%d,%d)
",t++,pre[x][y].x,pre[x][y].y,x,y);
if(map[x][y]!='.'&&map[x][y]!='X')
{
int i=map[x][y]-'0';
for(;i>0;i--)
{
printf("%ds:FIGHT AT (%d,%d)
",t++,x,y);
}
}
}
/*
//bfs路径输出模版输出
void print(int x,int y)
{
if(x==0&&y==0)
{
printf("(0, 0)
");
return ;
}
print(pre[x][y].x,pre[x][y].y);
printf("(%d, %d)
",x,y);
}
*/
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin>>map[i][j];
}
}
memset(visit,0,sizeof(visit));
ans=0;
t=1;
bfs();
if(ans==0)
{
printf("God please help our poor hero.
");
printf("FINISH
");
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.
",tt);
print(n-1,m-1);
printf("FINISH
");
}
}
}