#include <iostream>
#include <queue>
#include <stdlib.h>
using namespace std;
#define OBJECT -2
#define WALL 1
typedef struct{
int x,y,preX,preY,val;
}Node;
int step[50][50];//标识是否走过
//广度优先遍历找最短路径
Node bfs(Node **map, int n, int m){
int direc[][2] = {{-1,0},{0,-1},{1,0},{0,1}};//控制四个方向
map[0][0].preX = -1;map[0][0].preY = -1;
queue<Node> q;
q.push(map[0][0]);
while(!q.empty()){ //广度优先暴搜
Node cur = q.front();
for(int i = 0;i<4;i++){
int nextX = cur.x + direc[i][0];
int nextY = cur.y + direc[i][1];
if(nextX>=0 && nextX <m && nextY>=0 && nextY <n){//判断越界
if(map[nextX][nextY].val == OBJECT){
map[nextX][nextY].preX = cur.x;
map[nextX][nextY].preY = cur.y;
return map[nextX][nextY];
}else if(!step[nextX][nextY] && map[nextX][nextY].val != WALL){
map[nextX][nextY].preX = cur.x;
map[nextX][nextY].preY = cur.y;
q.push(map[nextX][nextY]);
step[nextX][nextY] = 1;
}
}
}
q.pop();
}
Node empty;
return empty;
}
int main(){
int n,m;
cin>>n>>m;
Node **map = (Node **)malloc(sizeof(Node*)*n);
for(int i = 0;i<n;i++){
map[i] = (Node *)malloc(sizeof(Node)*m);
}
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
int val;
cin>>val;
map[i][j].val = val;
map[i][j].x = i;
map[i][j].y = j;
}
}
Node object = bfs(map,n,m);
cout<<object.x<<","<<object.y<<endl;
int xx = object.preX;
int yy = object.preY;
while(xx!=0 || yy!=0){
cout<<xx<<","<<yy<<" -> ";
object = map[xx][yy];
xx = object.preX;
yy = object.preY;
}
cout<<"0,0"<<endl;
return 0;
}