#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
using namespace std;
int n, m;
int dir[4][2] = { { 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 } };
struct Node
{
int time;
int x, y;
int prex, prey;
char ch;
bool operator>(const Node & node) const
{
return time > node.time;
}
}node[105][105];
bool BFS()
{
int i;
int mx, my;
Node cur;
priority_queue<Node, vector<Node>, greater<Node> > pq;
node[0][0].time = 0;
pq.push(node[0][0]);
while (!pq.empty())
{
cur = pq.top();
pq.pop();
if (cur.x == n - 1 && cur.y == m - 1)
return true;
for (i = 0; i < 4; i++)
{
mx = cur.x + dir[i][0];
my = cur.y + dir[i][1];
if (mx >= 0 && mx < n&&my >= 0 && my < m)
{
if (node[mx][my].ch == '.'&&node[mx][my].time > cur.time + 1)
{
node[mx][my].time = cur.time + 1;
node[mx][my].prex = cur.x;
node[mx][my].prey = cur.y;
pq.push(node[mx][my]);
}
else if (node[mx][my].ch >= '1'&&node[mx][my].ch <= '9'&&node[mx][my].time > cur.time + node[mx][my].ch - '0'+1)//注意这里加1
{
node[mx][my].time = cur.time + node[mx][my].ch - '0' + 1;//注意这里加1
node[mx][my].prex = cur.x;
node[mx][my].prey = cur.y;
pq.push(node[mx][my]);
}
}
}
}
return false;
}
void printPath(int x, int y)
{
if (x == 0 && y == 0)
return;
int prex = node[x][y].prex;
int prey = node[x][y].prey;
printPath(prex, prey);
int prelength = node[prex][prey].time;
int length = node[x][y].time;
printf("%ds:(%d,%d)->(%d,%d)\n", prelength + 1, prex, prey, x, y);
for (int i = prelength + 2; i <= length; i++)
{
printf("%ds:FIGHT AT (%d,%d)\n", i, x, y);
}
}
int main()
{
int i, j;
while (~scanf("%d%d", &n, &m))
{
for (i = 0; i < n; i++)
{
getchar();
for (j = 0; j < m; j++)
{
scanf("%c", &node[i][j].ch);
node[i][j].time = INT_MAX;
node[i][j].x = i;
node[i][j].y = j;
}
}
bool state = BFS();
if (!state)
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
continue;
}
printf("It takes %d seconds to reach the target position, let me show you the way.\n", node[n - 1][m - 1].time);
printPath(n - 1, m - 1);
printf("FINISH\n");
}
return 0;
}