50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
from collections import deque
|
|
|
|
dx = [1, 0, -1, 0]
|
|
dy = [0, 1, 0, -1]
|
|
result = []
|
|
|
|
N=int(input())
|
|
arr = [[0] * N for i in range(N)]
|
|
visited = [[False] * N for i in range(N)]
|
|
|
|
for i in range(N):
|
|
numbers = list(input())
|
|
for j in range(N):
|
|
arr[i][j] = int(numbers[j])
|
|
|
|
|
|
def BFS(i, j):
|
|
chance = 0
|
|
queue = deque()
|
|
queue.append((i,j))
|
|
visited[i][j]=True
|
|
while queue:
|
|
now = queue.popleft()
|
|
chance += 1
|
|
for k in range(4):
|
|
x = now[0] + dx[k]
|
|
y = now[1] + dy[k]
|
|
if(x >= 0 and y >= 0 and x<N and y<N):
|
|
if (visited[x][y]==False and arr[x][y]==True):
|
|
visited[x][y]=True
|
|
arr[x][y]=arr[now[0]][now[1]]+1
|
|
queue.append((x,y))
|
|
return chance
|
|
|
|
|
|
for i in range(N):
|
|
for j in range(N):
|
|
if (not visited[i][j] and arr[i][j]!=0):
|
|
k=BFS(i,j)
|
|
if(k!=0):
|
|
result.append(k)
|
|
else:
|
|
visited[i][j]=True
|
|
|
|
result.sort()
|
|
|
|
print(len(result))
|
|
|
|
for i in range(len(result)):
|
|
print(result[i]) |