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 javafx.util.Pair;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.*;
  
  public final class Scratch {
      public static void main(String[] args)throws IOException {
          FastScanner scan = new FastScanner();
          //SuperFastScanner scan = new SuperFastScanner();
          PrintWriter out = new PrintWriter(System.out);
          StringBuilder result = new StringBuilder();
  
          HashMap<Integer, Direction> map = new HashMap<>();
          map.put(0,new Direction(0,-1));
          map.put(1,new Direction(1,0));
          map.put(2,new Direction(0,1));
          map.put(3,new Direction(-1,0));
  
          int t = 1;
          while (t-- > 0){
  
              int n = scan.nextInt();
              int k = scan.nextInt();
              int[][]input = new int[n][k];
  
              List<Integer> ans = new ArrayList<>();
              for (int i = 0; i < n; i++) {
                  input[i] = scan.nextIntArray(k);
              }
              boolean[][]traversed = new boolean[n][k];
              int r = 0;
              int c = 0;
              Queue<Direction> queue = new LinkedList<>();
  
              while (isValid(r,c,n,k)) {
                  int area = 0;
                  queue.add(new Direction(r,c));
                  while(!queue.isEmpty()) {
                      Direction pos = queue.poll();
                      if (traversed[pos.y][pos.x]) {
                          continue;
                      }
                      area++;
                      traversed[pos.y][pos.x] = true;
                      int num = input[pos.y][pos.x];
                      for (int i = 0; i < 4; i++) {
                          if (((num >> i) & 1) == 0) {
                              int x = pos.x + map.get(i).x;
                              int y = pos.y + map.get(i).y;
                              queue.add(new Direction(y,x));
                          }
                      }
                  }
                  if(area != 0)
                      ans.add(area);
                  c = (c+1) % (k);
                  if(c == 0)
                      r++;
              }
              Collections.sort(ans);
              Collections.reverse(ans);
              for (Integer area : ans
                   ) {
                  result.append(area+" ");
              }
              result.substring(0,result.length());
              //result.append(ans);
              result.append("\n");
  
          }
  
          out.println(result);
          out.flush();
      }
  
      static class Direction {
          int x;
          int y;
  
          public Direction(int y, int x) {
              this.x = x;
              this.y = y;
          }
      }
  
      private static boolean isValid(int r, int c, int n, int k) {
          if(r >= n || c >= k)
              return false;
          return true;
      }
  
      public static int solve(char[] num,int idx,char key){
  
          for(int i = num.length-idx;i>=0;i--){
              if(num[i]==key ){
                  return idx;
              }
              idx++;
          }
          return 20;
      }
  
      static class FastScanner{ //10^5 -- .15 sec && 4*10^6 ---> .86 sec
  
          BufferedReader br;
          StringTokenizer st;
  
          public FastScanner(){
              br = new BufferedReader(new InputStreamReader(System.in));
          }
          public String next(){
              while (st == null || !st.hasMoreElements()){
                  try{
                      st = new StringTokenizer(br.readLine());
                  }catch (IOException  e){
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
          public int nextInt(){
              return Integer.parseInt(next());
          }
          public long nextLong(){
              return Long.parseLong(next());
          }
          public double nextDouble(){
              return Double.parseDouble(next());
          }
          public String nextLine(){
              String str = "";
              try{
                  str = br.readLine();
              }catch (IOException e){
                  e.printStackTrace();
              }
              return str;
          }
          public int[] nextIntArray(int n){
              int arr[] = new int[n];
              for(int i = 0;i<n;i++){
                  arr[i] = nextInt();
              }
              return arr;
          }
          public long[] nextLongArray(int n){
              long arr[] = new long[n];
              for(int i = 0;i<n;i++){
                  arr[i] = nextLong();
              }
              return arr;
          }
          public double[] nextDoubleArray(int n){
              double arr[] = new double[n];
              for(int i = 0;i<n;i++){
                  arr[i] = nextDouble();
              }
              return arr;
          }
          public String[] nextStringArray(int n){
              String arr[] = new String[n];
              for(int i = 0;i<n;i++){
                  arr[i] = next();
              }
              return arr;
          }
      }
  }