#include <iostream>
#include <queue>
using namespace std;
struct cord
{
int x;
int y;
};
bool map[102][102];
int visited[102][102];
void input_map(int N, int M)
{
char c;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
{
cin >> c;
if (c == '1') map[i][j] = 1;
else map[i][j] = 0;
}
}
int solv_maze(int n, int m, cord s)
{
//int cnt = 0;
queue<cord> q;
q.push(s);
cord next;
cord now = q.front();
visited[now.y][now.x] = 1;
while (!q.empty())
{
now = q.front();
q.pop();
if (now.x == m && now.y == n) {
return visited[now.y][now.x];
}
if (map[now.y][now.x - 1] == 1 && visited[now.y][now.x - 1] == 0)
{
//cnt++;
next.y = now.y;
next.x = now.x - 1;
q.push(next);
visited[next.y][next.x] = visited[now.y][now.x] + 1;
}
if (map[now.y - 1][now.x] == 1 && visited[now.y - 1][now.x] == 0)
{
next.y = now.y - 1;
next.x = now.x;
q.push(next);
visited[next.y][next.x] = visited[now.y][now.x] + 1;
}
if (map[now.y][now.x + 1] == 1 && visited[now.y][now.x + 1] == 0)
{
next.y = now.y;
next.x = now.x + 1;
q.push(next);
visited[next.y][next.x] = visited[now.y][now.x] + 1;
}
if (map[now.y + 1][now.x] == 1 && visited[now.y + 1][now.x] == 0)
{
next.y = now.y + 1;
next.x = now.x;
q.push(next);
visited[next.y][next.x] = (visited[now.y][now.x] + 1);
}
}
}
int main()
{
int N, M;
cin >> N >> M;
cord start;
start.x = 1;
start.y = 1;
input_map(N, M);
cout << solv_maze(N, M, start);
//system("pause");
}