from collections import deque
def solution(maps):
n, m = len([i[0] for i in maps]), len(maps[0])
direction = [(1, 0), (-1, 0), (0, -1), (0, 1)]
q = deque()
q.append((0, 0))
while q:
x, y = q.popleft()
for d in direction:
x2 , y2 = x+d[0], y+d[1]
if 0<=x2<n and 0<=y2<m and maps[x2][y2]==1:
q.append((x2, y2))
maps[x2][y2] = maps[x][y] + 1
if maps[n-1][m-1]==1: return -1
return maps[n-1][m-1]