import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public class Graph {
public static final int[] dx = {0, 0, 1, -1};
public static final int[] dy = {1, -1, 0, 0};
static void bfs(int[][] arr, int[][] group, int x, int y, int n, int cnt){
Queue queue = new LinkedList();
queue.add(new Pair(x, y));
group[x][y] = cnt;
Pair temp = null;
while(!queue.isEmpty()){
temp = queue.remove();
for(int i=0; i<4; i++){
int nx = temp.x + dx[i];
int ny = temp.y + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < n){
if(group[nx][ny] == 0 && arr[nx][ny] == 1){
queue.add(new Pair(nx, ny));
group[nx][ny] = cnt;
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
int[][] a = new int[n][n];
int[][] group = new int[n][n];
String input = "";
for(int i=0; i<n; i++){
input = sc.nextLine();
for(int j=0; j<n; j++){
a[i][j] = input.charAt(j) - '0';
}
}
int cnt = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(a[i][j] == 1 && group[i][j] == 0){
bfs(a, group, i, j, n, ++cnt);
}
}
}
int[] ans = new int[cnt];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(group[i][j] != 0){
ans[group[i][j] - 1] += 1;
}
}
}
System.out.println(cnt);
for(int i=0; i<cnt; i++){
System.out.println(ans[i]);
}
}
}