import java.util.*;
import java.io.*;
class Main {
static int N ;
static int count;
static int dangCount = 0;
static int map[][];
static boolean check[][];
static int dx[] = { 0 , 1 , 0 , -1 };
static int dy[] = {1 , 0 , -1 , 0};
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new int [N+2][N+2];
check = new boolean[N+2][N+2];
int answer[] = new int[N*N];
for(int i = 1; i <= N ; i ++) {
String s = br.readLine();
for(int j = 0 ; j < N; j++) {
map[i][j+1] = s.charAt(j)-'0';
}
}
for (int i = 0; i < map.length; i++) {
for(int j = 0 ; j < map.length; j++) {
if(map[i][j] ==1 && !check[i][j] ) {
count = 0;
dfs(i,j);
answer[dangCount] = count;
dangCount ++;
}
}
}
sb.append(dangCount).append("\n");
Arrays.sort(answer, 0, dangCount);
for(int i = 0 ; i < dangCount ; i ++) {
sb.append(answer[i]).append("\n");
}
System.out.println(sb);
}
private static void dfs(int x, int y) {
count += 1;
check[x][y] = true;
for(int i = 0 ; i < 4; i ++) {
int nextX = x + dx[i];
int nextY = y + dy[i];
if(map[nextX][nextY] == 1 && !check[nextX][nextY]) {
dfs(nextX , nextY);
}
}
}
}