import java.util.Arrays;
import java.util.Scanner;
public class BOJ_2667_DFS {
static int[][] map;
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
map = new int[n][n];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < n; j++) {
map[i][j] = Integer.parseInt(s.charAt(j) + "");
}
}
solve();
}
private static void solve() {
int team = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == 1)
dfs(i, j, ++team);
}
}
System.out.println(Arrays.toString(map[0]));
System.out.println(Arrays.toString(map[1]));
System.out.println(Arrays.toString(map[2]));
System.out.println(Arrays.toString(map[3]));
System.out.println(Arrays.toString(map[4]));
System.out.println(Arrays.toString(map[5]));
System.out.println(Arrays.toString(map[6]));
int[] count = new int[team - 1];
for (int k = 0; k < n; k++) {
for (int l = 0; l < n; l++) {
if (map[k][l] > 1)
count[map[k][l] - 2]++;
}
}
Arrays.sort(count);
System.out.println(team - 1);
for (int h = 0; h < count.length; h++) {
if (count[h] != 0)
System.out.println(count[h]);
}
}
static int[] goX = {0, 1, 0, -1};
static int[] goY = {-1, 0, 1, 0};
private static void dfs(int i, int j, int team) {
map[i][j] = team;
for (int k = 0; k < 4; k++) {
int x = i + goX[k];
int y = j + goY[k];
if ((-1 < x && x < n) && (-1 < y && y < n) && map[x][y] == 1)
dfs(x, y, team);
}
}
}