import sys
n = int(sys.stdin.readline().rstrip())
town = [[] for _ in range(n)]
count = []
dx = [1,0,-1,0]
dy = [0,1,0,-1]
dange = 2
for i in range(n):
town[i] += map(int,sys.stdin.readline().rstrip())
from collections import deque
def BFS(startX , startY):
queue = deque([[startX,startY]])
town[startX][startY] = dange
c = 1
while queue:
pop = queue.popleft()
for i in range(4):
nextX = pop[0] + dx[i]
nextY = pop[1] + dy[i]
if(0<= nextX < n and 0 <= nextY < n and town[nextX][nextY] ==1 ):
queue.append([nextX,nextY])
town[nextX][nextY] = dange
c+=1
count.append(c)
for i in range(n):
for j in range(n):
if(town[i][j] ==1 ):
BFS(i,j)
dange += 1
print(len(count))
count.sort()
for i in count:
print(i)