import sys
sys.setrecursionlimit(10000)
n = int(input())
map_ = []
area_cnt = 0
house_cnts = []
def is_valid(x,y):
if x >= 0 and y >= 0 and x < n and y < m:
return True
return False
def dfs(x,y):
global house_cnt
if not is_valid(x,y) :
return False
if map_[x][y] == 1:
map_[x][y] = 0
dfs(x+1,y)
dfs(x-1,y)
dfs(x,y+1)
dfs(x,y-1)
house_cnt += 1
for _ in range(n):
map_.append(list(map(int, input())))
m = len(map_[0])
for x in range(n):
for y in range(m):
house_cnt = 0
if map_[x][y] == 1:
area_cnt += 1
dfs(x,y)
house_cnts.append(house_cnt)
print(area_cnt)
for cnt in sorted(house_cnts):
print(cnt)