from collections import deque
import sys
import copy
N, M = map(int, sys.stdin.readline().split())
arr = [[0]]
visit = [[0 for j in range(1001)]for i in range(1001)]
storage = [] # 경로값 저장
for i in range(N):
arr.append([0]+list(sys.stdin.readline()))
def findroute(): # 0인 곳만을 찾아가는 함수..
visit2 = copy.deepcopy(visit)
visit2[1][1] = 1
q = deque()
q.append((1, 1))
while q:
x, y = q.popleft()
if x == N and y == M:
storage.append(visit2[N][M])
break
if x - 1 >= 1:
if arr[x - 1][y] == 0 and visit2[x-1][y] == 0:
q.append((x - 1, y))
visit2[x - 1][y] = visit2[x][y] + 1
if x + 1 <= 1000:
if arr[x + 1][y] == 0 and visit2[x+1][y] == 0:
q.append((x + 1, y))
visit2[x + 1][y] = visit2[x][y] + 1
if y - 1 >= 1:
if arr[x][y - 1] == 0 and visit2[x][y-1] == 0:
q.append((x, y - 1))
visit2[x][y - 1] = visit2[x][y] + 1
if y + 1 <= 1000:
if arr[x][y + 1] == 0 and visit2[x][y+1] == 0:
q.append((x, y + 1))
visit2[x][y + 1] = visit2[x][y] + 1
for i in range(1, N+1):
for j in range(1, M+1):
if arr[i][j] == 1:
arr[i][j] = 0
findroute()
arr[i][j] = 1
print(storage)
effort98 2년 전