from collections import deque h,w=map(int,input().split()) g=[list(map(int,input().split())) for _ in range(h)] def bfs(i,j): g[i][j]=0 q=deque() q.append((i,j)) while q: i,j=q.popleft() dy=[0,1,0,-1] dx=[1,0,-1,0] for k in range(4): y=i+dy[k] x=j+dx[k] if not(0<=y