import Foundation
let n = Int(readLine()!)!
var groupId = 0
var dx: [Int] = [-1, 0, 1, 0] // 만약 (0,1)이 아파트라면 그 주위 동서남북에 있는 아파트들으 체크하기 위해서 사용
var dy: [Int] = [0, 1, 0, -1]
var arr: [[String]] = Array(repeating: Array(repeating: "", count: n), count: n)
var visited: [[Bool]] = Array(repeating: Array(repeating: false, count: n), count: n)
var groups: [Int] = Array(repeating: 0, count: 25)
var result: [Int] = []
func dfsRecursion(_ x: Int, _ y: Int) {
visited[x][y] = true
groups[groupId] += 1
for i in 0..<4 {
let nx = x + dx[i]
let ny = y + dy[i]
if 0 <= nx && nx < n && 0 <= ny && ny < n {
if Int(arr[nx][ny])! == 1 && visited[nx][ny] == false {
dfsRecursion(nx, ny)
}
}
}
}
for i in 0..<n {
let strArr = Array(readLine()!)
for j in 0..<strArr.count {
arr[i][j] = String(strArr[j])
}
}
for i in 0..<n {
for j in 0..<n {
if Int(arr[i][j])! == 1 && visited[i][j] == false {
groupId += 1
dfsRecursion(i, j)
}
}
}
print(groupId)
for i in 1...groupId {
result.append(groups[i])
}
result.sorted().map { print($0) }