description: You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. 
  
  Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. 
  
  It is guaranteed that the construction always has walls on it's edges. The input will be correct. 
  
  Your task is to print the size of the rooms from biggest to smallest. 
  
  Input
  
  The first line has two numbers which are N and M, the size of the construction. Both are integers: 
  
  n (1 ≤ n ≤ 10^3)
  
  m (1 ≤ m ≤ 10^3)
  
  Next N x M numbers represent each tile of construction.
  
  Output
  
  Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. 
  
  Example
  
  Input
  
  
  4 5
  9 14 11 12 13
  5 15 11 6 7
  5 9 14 9 14
  3 2 14 3 14
  
  
  Output
  
  
  9 4 4 2 1 
solution: #include <bits/stdc++.h>
  using namespace std;
  int currSize;
  class tile {
   public:
    bool n, s, e, w;
    bool visited = false;
    int i, j;
    void make_walls(int n);
    void print() {
      cout << "(" << i << "," << j << ") "
           << "n " << n << " "
           << "e " << e << " "
           << "s " << s << " "
           << "w " << w << " " << endl;
    }
  };
  tile tiles[1000][1000];
  void tile::make_walls(int x) {
    w = x & 1;
    s = (x >> 1) & 1;
    e = (x >> 2) & 1;
    n = (x >> 3) & 1;
  }
  void dfs(int i, int j) {
    if (tiles[i][j].visited) return;
    tiles[i][j].visited = true;
    currSize++;
    tile t = tiles[i][j];
    if (!t.n) dfs(i - 1, j);
    if (!t.e) dfs(i, j + 1);
    if (!t.s) dfs(i + 1, j);
    if (!t.w) dfs(i, j - 1);
  }
  void solve() {
    int n, m, x;
    cin >> n >> m;
    vector<int> sizes;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        tile temp;
        cin >> x;
        temp.make_walls(x);
        temp.i = i;
        temp.j = j;
        tiles[i][j] = temp;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (!tiles[i][j].visited) {
          currSize = 0;
          dfs(i, j);
          sizes.push_back(currSize);
        }
      }
    }
    sort(sizes.begin(), sizes.end(), greater<int>());
    for (int x : sizes) {
      cout << x << " ";
    }
  }
  int main() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int t = 1;
    while (t--) {
      solve();
    }
    return 0;
  }
  