문제 링크 - https://www.acmicpc.net/problem/2667
from collections import deque
n = int(input())
graph = []
for _ in range(n):
graph.append(list(map(int, input())))
result = []
def bfs(graph, a, b):
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
global cnt_apt
graph[a][b] = 0
queue = deque()
queue.append((a,b))
cnt_apt+=1
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= n or ny < 0 or ny >= n:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = 0
queue.append((nx, ny))
cnt_apt += 1
cnt = 0
result = []
for a in range(n):
for b in range(n):
cnt_apt = 0
if graph[a][b] == 1:
bfs(graph, a, b)
cnt += 1
result.append(cnt_apt)
print(cnt)
for i in sorted(result):
print(i)