https://www.acmicpc.net/problem/2667
import java.util.*;
import java.io.*;
public class Main {
static int[][] arr;
static boolean[][] visited;
static int N, count;
static ArrayList<Integer> result;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
arr = new int[N][N];
visited = new boolean[N][N];
result = new ArrayList<>();
for (int i = 0; i < N; i++) {
String str = br.readLine();
for (int j = 0; j < N; j++) {
arr[i][j] = str.charAt(j) - '0';
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (arr[i][j] == 1 && !visited[i][j]) {
count = 0;
dfs(i, j);
result.add(count);
}
}
}
Collections.sort(result);
bw.write(String.valueOf(result.size()));
bw.newLine();
for (Integer integer : result) {
bw.write(String.valueOf(integer));
bw.newLine();
}
bw.flush();
}
public static void dfs(int y, int x) {
visited[y][x] = true;
count++;
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if (newX < 0 || newX >= N || newY < 0 || newY >= N) continue;
if (arr[newY][newX] == 1 && !visited[newY][newX])
dfs(newY, newX);
}
}
}