https://school.programmers.co.kr/learn/courses/30/lessons/250136?language=python3
from collections import deque
def solution(land):
N = len(land)
M = len(land[0])
visited = [[False for _ in range(M)] for _ in range(N)]
result = [0 for _ in range(M)]
def bfs(x,y):
Q = deque([(x,y)])
visited[x][y] = True
dx = [-1,1,0,0]
dy = [0,0,-1,1]
y_list = set()
count = 1
while Q:
x,y = Q.popleft()
y_list.add(y)
for i in range(4):
nx,ny = x + dx[i], y + dy[i]
if 0<=nx<N and 0<=ny<M and visited[nx][ny] == False and land[nx][ny]==1:
count += 1
Q.append((nx,ny))
visited[nx][ny] = True
for c in y_list:
result[c] += count
for i in range(N):
for j in range(M):
if visited[i][j] == False and land[i][j] ==1:
bfs(i,j)
return max(result)