#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

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

int arr[30][30];

int b = 0;
void change(int i,int j,int n){
    arr[i][j] = 0;
    for (int k = 0;k < 4;k++) {
        if(i+dy[k] >=0 && i+dy[k]< n && j+dx[k] >=0 && j+dx[k]< n){
            if (arr[i+dy[k]][j+dx[k]] == 1) {
                change(i+dy[k], j+dx[k],n);
                b++;
            }
        }
    }
}

int main() {
    int n;
    cin >> n;

    string im_string;
    int bone[30] = {0,};
    for (int i = 0;i < n;i++) {
        cin >> im_string; 
        for (int j = 0;j < n;j++) {
            arr[i][j] = im_string[j] - '0';
        }
    }
    
    int cnt = 0;
    int idx = 0;
    
    for (int i = 0;i < n;i++) {
        for (int j = 0;j < n;j++) {
            if (arr[i][j] == 1) {
                change(i,j,n);
                cnt++;
                bone[idx] = b;
                idx++;
                b = 0;
            }
        }
    }

    cout << cnt << "\n";
    sort(bone,bone+25);
    for (int i = 0;i < 25;i++) {
        if (bone[i] != 0) {
            cout << bone[i] << "\n";
        }
    }
    return 0;
}

Embed on website

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