#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};

int N, house;
vector<vector<int>> map;
vector<vector<bool>> visited;

void dfs(int X, int Y) {
    house += 1;
    visited[Y][X]=true;

    for(int k=0;k<4;k++) {
        int nx = X + dx[k];
        int ny = Y + dy[k];

        if(nx >= 0 && nx < N && ny >= 0 && ny < N) {
            if(map[ny][nx]==1 && visited[ny][nx]==false) {
                dfs(nx, ny);
            }
        }  
    }
}

int main() {
    cin >> N;

    map = vector<vector<int>>(N, vector<int>(N, 0));
    visited = vector<vector<bool>>(N, vector<bool>(N, false));

    for(int i=0;i<N;i++) {
        string s;
        cin >> s;
        for(int j=0;j<N;j++) {
            map[i][j]=s[j] - '0';
        }
    }

    int count=0;
    vector<int> House;

    for(int i=0;i<N;i++) {
        for(int j=0;j<N;j++) {
            if(map[i][j]==1 && visited[i][j]==false) {
                house = 0;
                dfs(j, i);   // ✅ 수정

                count += 1;
                House.push_back(house);
            }
        }
    }

    sort(House.begin(), House.end());

    cout << count << '\n';  // ✅ 수정

    for(int i=0;i<House.size();i++) {
        cout << House[i] << "\n";
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: