import java.util.*;
import java.io.*;
public class Main
{
static int[][] graph;
static int[] dx = {0,1,0,-1};
static int[] dy = {-1,0,1,0};
static int n;
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
graph = new int[n][n];
for(int i = 0; i < n; i++){
String[] str = br.readLine().split("");
for(int j = 0; j < n ;j++){
graph[i][j] = Integer.parseInt(str[j]);
}
}
ArrayList<Integer> result = new ArrayList<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(graph[i][j] == 1){
result.add(BFS(i,j));
}
}
}
Collections.sort(result);
System.out.println(result.size());
for(int a : result)
System.out.println(a);
//System.out.print(Arrays.deepToString(graph));
}
static int BFS(int y, int x){
Queue<int[]> queue = new LinkedList<>();
graph[y][x] = 0;
int count = 1;
queue.add(new int[]{y,x});
while(queue.size() > 0){
int[] current = queue.poll();
for(int i = 0; i < 4; i++){
int nextY = current[0] + dy[i];
int nextX = current[1] + dx[i];
if(0 <= nextY && nextY < n && 0 <= nextX && nextX < n)
{
if(graph[nextY][nextX] == 1){
graph[nextY][nextX] = 0;
count++;
queue.add(new int[]{nextY, nextX});
}
}
}
}
return count;
}
}[소프티어 2Level] 장애물 인식 프로그램(Java)
2023. 8. 3. 15:37