# your code goes here
from collections import deque


def bfs(x, y):
    global a
    a = 0
    need_visited = deque()
    need_visited.append((x, y))
    dx = (1, -1, 0, 0)
    dy = (0, 0, 1, -1)
    while need_visited:
        x, y = need_visited.popleft()
        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]
            if (0 <= nx < N and 0 <= ny < N) and graph[nx][ny] == 1:
                a += 1
                graph[nx][ny] = 0
                need_visited.append((nx, ny))
    A.append(a)


N = int(input())
A = []
ans = 0
graph = [list(map(int, input())) for _ in range(N)]
for i in range(N):
    for j in range(N):
        if graph[i][j] == 1:
            ans += 1
            bfs(i, j)
print(ans)
A.sort()
for i in A:
    print(i)
