https://www.acmicpc.net/problem/2667
def grouping(i, j):
graph[i][j] = 2
cnt = 0
if graph[i-1][j] == 1:
cnt += grouping(i-1, j)
if graph[i+1][j] == 1:
cnt += grouping(i+1, j)
if graph[i][j-1] == 1:
cnt += grouping(i, j-1)
if graph[i][j+1] == 1:
cnt += grouping(i, j+1)
return cnt + 1
n = int(input())
graph = []
graph.append([0]*(n+2))
for _ in range(n):
graph.append([0]+list(map(int, list(input())))+[0])
graph.append([0]*(n+2))
result = []
for i in range(1,n+1):
for j in range(1,n+1):
if graph[i][j]==1:
cnt = grouping(i,j)
result.append(cnt)
result.sort()
print(len(result))
for r in result:
print(r)