from collections import deque
import sys
input = sys.stdin.readline
n,m = map(int, input().split())
map = [list(map(int, input().strip())) for _ in range(n)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def bfs(y,x):
q=deque()
q.append((y,x))
cnt = 0
while q:
ey, ex = q.popleft()
for k in range(4):
xx = dx[k] + ex
yy = dy[k] + ey
if 0 <= yy < n and 0 <= xx < m and map[yy][xx] == 1:
map[yy][xx] = map[ey][ex]+1
q.append((yy,xx))
bfs(0,0)
print(map[-1][-1])
'자기개발👨💻 > 코딩 알고리즘' 카테고리의 다른 글
[C++] 백준 2056 작업 (0) | 2023.11.02 |
---|---|
[Python] 백준 2559 수열 - 그리디 알고리즘 (0) | 2023.10.24 |
[Python] 백준 2559 수열 - 투포인터 알고리즘 (0) | 2023.10.24 |
[Python] 백준 15650, 15649 -백트래킹 알고리즘 (0) | 2023.10.18 |
[C] 백준 10870 피보나치 수 5 (0) | 2022.08.03 |
이진탐색 문제리스트 (0) | 2022.05.27 |