
from collections import deque
dx = [0, 0, 1, -1]
dy = [1, -1 , 0, 0]
def bfs(arr, i, j):
N = len(arr)
queue = deque()
queue.append((i, j))
arr[i][j] = 0
count = 1
while queue:
x, y = queue.popleft()
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if nx < 0 or nx >= N or ny < 0 or ny >= N:
continue
if arr[nx][ny] == 1:
arr[nx][ny] = 0
queue.append((nx, ny))
count += 1
return count
N = int(input())
arr = []
for i in range(N):
arr.append(list(map(int, input())))
cnt = []
for i in range(N):
for j in range(N):
if arr[i][j] == 1:
cnt.append(bfs(arr, i, j))
print(len(cnt))
cnt.sort()
for i in cnt:
print(i)