

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import sys from collections import deque dy = [1, –1, 0, 0] dx = [0, 0, –1, 1] def bfs(): queue = deque([(0, 0)]) while queue: y, x = queue.popleft() for i in range(4): ny = y + dy[i] nx = x + dx[i] if 0 <= nx < m and 0 <= ny < n and arr[ny][nx] == 1: queue.append((ny, nx)) arr[ny][nx] = arr[y][x] + 1 n, m = map(int, sys.stdin.readline().rstrip().split()) arr = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(n)] bfs() print(arr[n – 1][m – 1]) | cs |