import java.util.*;
public class main {
public static class Pair {
public int x, y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public static int n, m;
public static boolean is_valid(long x, long y){
return x < n && y < m && x >= 0 && y >= 0;
}
public static void main
( String[] args
){
Scanner scanner
= new Scanner
(System.
in);
n = scanner.nextInt();
m = scanner.nextInt();
long [][] a = new long[n][m];
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m ; j++)
a[i][j] = scanner.nextInt();
LinkedList<Pair> q = new LinkedList<Pair>();
q.push(new Pair(0, 0));
long [][] d = new long [n][m];
Pair v;
boolean [][] ok = new boolean[n][m];
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m ;j++)
ok[i][j] = false;
int x, y;
d[0][0] = 0;
ok[0][0] = true;
int [][] adj = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
while(!q.isEmpty()){
v = q.pop();
x = v.x;
y = v.y;
ok[x][y] = true;
for(int i = 0 ; i < 4 ; i++){
if(!is_valid(x + adj[i][0], y + adj[i][1])) continue;
if(a[x+adj[i][0]][y+adj[i][1]] == 1) continue;
if(ok[x+adj[i][0]][y+adj[i][1]]) continue;
d[x+adj[i][0]][y+adj[i][1]] = 1 + d[x][y];
q.push(new Pair((x+adj[i][0]), (y+adj[i][1])));
}
}
for(int i = 0 ; i < n ;i++)
for(int j = 0 ; j < m ; j++)
if(a[i][j] == 9)
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}