import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
static String[] a;
static int n;
static boolean[][] visit;
static ArrayList<Integer> group;
static int groupCount;
static int[][] dir = {{-1,0},{1,0},{0,-1},{0,1}};
static void DFS(int x, int y) {
groupCount++;
visit[x][y] = true;
for(int i=0; i<4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(nx<0 || ny<0 || ny>=n || nx>=n) continue;
if(visit[nx][ny] || a[nx].charAt(ny)=='0') continue;
DFS(nx, ny);
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
n = kb.nextInt();
kb.nextLine();
a = new String[n];
for(int i=0; i<n; i++) {
a[i] = kb.nextLine();
}
visit = new boolean[n][n];
group = new ArrayList<>();
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(a[i].charAt(j)=='1' && !visit[i][j]) {
groupCount =0;
DFS(i, j);
group.add(groupCount);
}
}
}
Collections.sort(group);
System.out.println(group.size());
for(int x: group) {
System.out.println(x);
}
}
}