import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int[] dy = {0, 0, 1, -1};
static int[] dx = {1, -1, 0, 0};
static int N, res;
static int[][] map;
static List<Integer> list;
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][N];
for(int i=0; i<N; i++){
String str = br.readLine();
for(int j=0; j<N; j++){
map[i][j] = Integer.parseInt(str.charAt(j)+"");
}
}
res = 0;
list = new ArrayList<>();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(map[i][j]==1){
list.add(dfs(i,j));
res++;
}
}
}
System.out.println(res);
Collections.sort(list);
for(Integer num: list){
System.out.println(num);
}
}
static int dfs(int startY, int startX){
int cnt = 1;
Deque<int[]> queue = new ArrayDeque<>();
map[startY][startX] = 0;
queue.offer(new int[] {startY, startX});
while(!queue.isEmpty()){
int[] items = queue.poll();
int y = items[0];
int x = items[1];
for(int d=0; d<4; d++){
int ny = y+dy[d];
int nx = x+dx[d];
if(ny<0 || ny >=N || nx<0 || nx >= N) continue;
if(map[ny][nx] == 1){
map[ny][nx] = 0;
queue.offer(new int[] {ny, nx});
cnt++;
}
}
}
return cnt;
}
}