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: import java.util.*;
  import java.io.*;
  
  public class Rextester {
      static final FS sc          = new FS();
      static final PrintWriter pw = new PrintWriter(System.out);
      static final Util u         = new Util();
      
      public static void main(String[] args) {
          int testCases = 1; // sc.nextInt();
          while(testCases-- > 0) {
              int n = sc.nextInt();
              int m = sc.nextInt();
              
              int[][] arr = new int[n][m];
              for(int i = 0; i<n; i++) {
                  for(int j = 0; j<m; j++) {
                      arr[i][j] = sc.nextInt();
                  }
              }
              
              List<Integer> ans = new ArrayList<>();
              boolean[][] visited = new boolean[n][m];
              for(int i = 0; i<n; i++) {
                  for(int j = 0; j<m; j++) {
                      if(!visited[i][j])  ans.add(dfs(i, j, arr, visited, 'x'));
                  }
              }
              Collections.sort(ans);
              Collections.reverse(ans);
              for(int i = 0; i<ans.size(); i++) pw.print(ans.get(i)+" ");
              pw.println();
          }
          pw.flush();
      }
      
      static int dfs(int i, int j, int[][] arr, boolean[][] visited, char inComingDir) {
          if(visited[i][j]) return 0;
          
          visited[i][j] = true;
          
          // if(outOfZone(i,j, arr.length; arr[0].length)) no need given in question
          //    return 0;
          
          if(inComingDir != 'x' && !isOpen(inComingDir, arr[i][j]))
              return 0;
          
          int ans = 1;
          if(isOpen('n', arr[i][j])) ans += dfs(i-1, j, arr, visited, 's');
          if(isOpen('s', arr[i][j])) ans += dfs(i+1, j, arr, visited, 'n');
          if(isOpen('e', arr[i][j])) ans += dfs(i, j+1, arr, visited, 'w');
          if(isOpen('w', arr[i][j])) ans += dfs(i, j-1, arr, visited, 'e');
          
          return ans;
      }
      
          
      
      static boolean isOpen(char inComingDir, int val) {
          int pos = -1;
          if(inComingDir == 'n') pos = 4;
          if(inComingDir == 'e') pos = 3;
          if(inComingDir == 's') pos = 2;
          if(inComingDir == 'w') pos = 1;
          
          if((val & (1<<(pos-1))) == 0) return true; else return false;
      }
      
      
      
      //------------------------------Utility-Classes----------------------------//
      static class FS {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st = new StringTokenizer("");
          String next() {
              while(!st.hasMoreElements()) {
                  try {
                      st = new StringTokenizer(br.readLine());
                  } catch(Exception e) {}
              }
              return st.nextToken();
          }
          int nextInt() {
              return Integer.parseInt(next());
          }
          long nextLong() {
              return Long.parseLong(next());
          }
      }
      static class Util {
          
          void sc1D(int[] a) {
              for(int i = 0; i<a.length; i++)
                      a[i] = sc.nextInt();
          }
  
          void sc2D(int[][] a) {
              for(int i = 0; i<a.length; i++)
                  for(int j = 0; j<a[0].length; j++)
                          a[i][j] = sc.nextInt();
          }
          
          int gcd(int number1, int number2) {
              if (number2 == 0)
                  return number1;
              return gcd(number2, number1 % number2);
          }
          
      }
  }