37 lines
970 B
Python
37 lines
970 B
Python
import sys
|
|
from collections import deque
|
|
|
|
input = sys.stdin.readline
|
|
|
|
def BFS(filedMap, visited, x, y, N):
|
|
queue = deque([(x,y)])
|
|
visited[y][x] = True
|
|
cnt = 1
|
|
|
|
dx = [1,-1,0,0]
|
|
dy = [0,0,1,-1]
|
|
|
|
while len(queue):
|
|
cx, cy = queue.popleft()
|
|
for i in range(4):
|
|
nx, ny = cx + dx[i], cy + dy[i]
|
|
if 0<=nx<N and 0<=ny<N and not visited[ny][nx] and filedMap[ny][nx]==1:
|
|
queue.append((nx,ny))
|
|
visited[ny][nx] = True
|
|
cnt += 1
|
|
return cnt
|
|
|
|
N = int(input())
|
|
filedMap = []
|
|
visited = [[False]*N for _ in range(N)]
|
|
for _ in range(N):
|
|
filedMap.append(list(map(int,[s for s in input().rstrip()])))
|
|
|
|
cntEstate = []
|
|
for i in range(N):
|
|
for j in range(N):
|
|
if not visited[i][j] and filedMap[i][j] > 0 :
|
|
cntEstate.append(BFS(filedMap, visited, j, i, N))
|
|
|
|
result = str(len(cntEstate)) + '\n' + '\n'.join(map(str, sorted(cntEstate)))
|
|
print(result) |