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: //package Java;
  import java.io.*;
  import java.text.DecimalFormat;
  import java.util.*;
  import java.util.Map.Entry;
  
  public class Main {
  	static int c=0;
  	static final PrintWriter out = new PrintWriter(System.out, true);
      static final FastScanner sc = new FastScanner();
  	public static void main(String[] args)  {
  		int n=sc.nextInt();
  		int m=sc.nextInt();
  		int a[][]=new int[n][m];
  		for(int i=0;i<n;i++) {
  			for(int j=0;j<m;j++) {
  				a[i][j]=sc.nextInt();
  			}
  		}
  		pairs g[][]=new pairs[n][m];
  		for(int i=0;i<n;i++) {
  			for(int j=0;j<m;j++) {
  				String l=Integer.toBinaryString(a[i][j]);
  				if(l.length()==1)l="000"+l;
  				if(l.length()==2)l="00"+l;
  				if(l.length()==3)l="0"+l;
  //				System.out.print(l+" ");
  				g[i][j]=new pairs(l.charAt(0)-'0',l.charAt(1)-'0',l.charAt(2)-'0',l.charAt(3)-'0');
  			}
  //			System.out.println();
  		}
  		ArrayList<Integer>ans=new ArrayList<>();
  		boolean v[][]=new boolean[n][m];
  		for(int i=0;i<n;i++) {
  			for(int j=0;j<m;j++) {c=0;
  				if(v[i][j]==false) {
  					dfs(i,j,n,m,a,g,v);
  					ans.add(c);
  				}
  			}
  		}
  		Collections.sort(ans,Collections.reverseOrder());
  		for(int y:ans)out.print(y+" ");
  		out.println();
  		out.close();
  	} 
  	static void dfs(int i,int j,int n,int m,int a[][],pairs g[][],boolean v[][]) {
  //		System.out.println(i+" "+j+" "+c);
  		if(i<0||i>=n||j<0||j>=m||v[i][j]==true) return;
  		//nesw
  		v[i][j]=true;
  		c++;
  		if(i+1<n) {
  			if(g[i][j].s==0&&g[i+1][j].n==0) {
  //				System.out.println("s");
  				dfs(i+1,j,n,m,a,g,v);
  			}
  		}
  		if(j+1<m) {
  			if(g[i][j].e==0&&g[i][j+1].w==0) {
  //				System.out.println("e");
  				dfs(i,j+1,n,m,a,g,v);
  			}
  		}
  		if(i-1>=0) {
  			if(g[i][j].n==0&&g[i-1][j].s==0) {
  //				System.out.println("n");
  				dfs(i-1,j,n,m,a,g,v);
  			}
  		}
  		if(j-1>=0) {
  			if(g[i][j].w==0&&g[i][j-1].e==0) {
  //				System.out.println("w");
  				dfs(i,j-1,n,m,a,g,v);
  			}
  		}
  	}
      static void nextPermutation(int[] nums) {
          int i = nums.length - 2;
          while (i >= 0 && nums[i + 1] <= nums[i]) {
              i--;
          }
          if (i >= 0) {
              int j = nums.length - 1;
              while (j >= 0 && nums[j] <= nums[i]) {
                  j--;
              }
              swap(nums, i, j);
          }
          reverse(nums, i + 1);
      }
  
      static void reverse(int[] nums, int start) {
          int i = start, j = nums.length - 1;
          while (i < j) {
              swap(nums, i, j);
              i++;
              j--;
          }
      }
  
      static void swap(int[] nums, int i, int j) {
          int temp = nums[i];
          nums[i] = nums[j];
          nums[j] = temp;
      }
  	boolean check(int pre[],int a[],int i,int m[]) {
  		if(pre[i]<i) return true;
  		return false;
  	}
  static long Cr(int n, int r,int mod,long fac[],long inv[]) {
      	if(n<r) return 0L;
  		return fac[n]*(inv[r]*inv[n - r]%mod)%mod;
   
  //    	long fac[]=new long[100000];
  //    	long inv[]=new long[100000];
  //    	fac[0]=fac[1]=1;
  //    	fac[i]=modmul(fac[i-1],i,m);
  //		inv[i] =modpow(fac[i], m - 2,m);
  //    	long ans = 1;
  //	    ans *= (Cr(n-x,more,m,fac,inv)*fac[more])%m;
  //	    ans %= m;
  }
  	static boolean [] sieveOfEratosthenes(int n){
  	    boolean prime[] = new boolean[n + 1];
  	    for (int i = 0; i <= n; i++)
  	        prime[i] = true;
  	
  	    for (int p = 2; p * p <= n; p++)
  	    {
  	        if (prime[p] == true)
  	        {
  	            for (int i = p * p; i <= n; i += p)
  	                prime[i] = false;
  	        }
  	    }
  	    prime[1]=prime[0]=false;
  	    return prime;
  	}
  	static void divisors(int n,ArrayList a[]) {
  		for(int i=1;i<=n;i++) {
  			for(int j=i;j<=n;j+=i) {
  				a[j].add(i);
  			}
  		}
  	}
  	 static long modDivide(long a, long b, int m)
  	  {
  	      a = a % m;
  	      long inv = modInverse(b, m);
  	      if (inv == -1) return -1;
  	      else
  	      {
  	        long c = (inv * a) % m ;
  	        return c;
  	      }
  	  }
  	  static long modInverse(long a, int m){
  	        long g = gcd(a, m);
  	        if (g != 1) return -1;
  	        else
  	        {
  	        	return modpow(a,m-2,m);
  	        }
  	    }
  	  public static double logx(long m,int x){
  	        double result = (double)(Math.log(m) / Math.log(x));
  	  
  	        return result;
  	    }
  	     static double setPrecision(double ans,int k){
  	    	 double y=Math.pow(10,k);
  	    	 return Math.round(ans*y)/y;
  	     }
  	  public static double log2(double m)
  	    {
  	        double result = (double)(Math.log(m) / Math.log(2));
  	  
  	        return result;
  	    }
  	  static int countDivisors(int n)
  	    { int ans=0;
  	        for (int i=1; i<=Math.sqrt(n); i++)
  	        {
  	            if (n%i==0)
  	            {
  	                if (n/i == i) ans++;
  //	                    System.out.print(" "+ i);
  	      
  	                else ans+=2;
  //	                    System.out.print(i+" " + n/i + " " );
  	            }
  	        }
  			return ans;
  	    }
  	  static ArrayList<Integer> pyyy = new ArrayList<Integer>();
  		static void sieveppp(int MAX) 
  		{ 
  		    int[] isPrime=new int[MAX+1]; 
  		  
  		    for (int i = 2; i<= MAX; i++) 
  		    { 
  		        if (isPrime[i] == 0) 
  		        { 
  		        	pyyy.add(i); 
  		            for (int j = 2; i * j<= MAX; j++) 
  		                isPrime[i * j]= 1; 
  		        } 
  		    } 
  		} 
  		 static int phi(int n) { 
  			 ///   call sieveppp(n) in main fn
  		      int res = n; 
  		      for (int i=0; pyyy.get(i)*pyyy.get(i) <= n; i++) 
  		      { 
  		          if (n % pyyy.get(i)== 0) 
  		          { 
  		              res -= (res / pyyy.get(i)); 
  		              while (n % pyyy.get(i)== 0) 
  		              n /= pyyy.get(i); 
  		          } 
  		      } 
  		      if (n > 1) 
  		      res -= (res / n); 
  		    
  		      return res; 
  		  } 
  	public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is in worst O(n^2)
  		//Collections.sort() uses merge sort
  		ArrayList<Long> ttttt = new ArrayList<Long>();
  		for(long y: arr)ttttt.add(y);Collections.sort(ttttt);
  		for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i);
  	}
  	public static void sort(int[] arr){//because Arrays.sort() uses quicksort which is dumb
  		//Collections.sort() uses merge sort
  		ArrayList<Integer> ttttt = new ArrayList<>();
  		for(int y: arr)ttttt.add(y);Collections.sort(ttttt);
  		for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i);
  	}
  	static boolean check(double[] a,double m,int n,int k) {
  		int ans=0;
  		for(int i=0;i<n;i++) {
  			ans+=(Math.floor(a[i]/m));
  			if(ans>=k) return true;
  		}
  		return ans>=k;
  	}
  	     static boolean isKthBitSet(long n, int k) { 
  			if ((n & (1 << (k - 1))) > 0) 
  			return true;
  			else
  			return false;
  			
  		 } 
  	     static long modpow(long a,long b,int m) {
  	    	 if(b==0) return 1;
  	    	 if(b==1) return a;
  	    	 long ans=0;
  	    	 long t=modpow(a,b/2,m);
  	    	 ans=modmul(t,t,m);
  	    	 if(b%2!=0) ans=modmul(a,ans,m);
  	    	 return ans;
  	     }
  		static long modmul(long a,long b,int m) {
  			return mod(mod(a,m)*mod(b,m),m);
  		}
  		static long mod(long x,int m) {
  			return ((x%m)+m)%m;
  		}
  		 static int octaltodecimal(int deciNum)
  		    {
  		        int octalNum = 0, countval = 1;
  		        int dNo = deciNum;
  		        while (deciNum != 0) {
  		 
  		            // decimals remainder is calculated
  		            int remainder = deciNum % 8;
  		 
  		            // storing the octalvalue
  		            octalNum += remainder * countval;
  		 
  		            // storing exponential value
  		            countval = countval * 10;
  		            deciNum /= 8;
  		        }
  		       return octalNum;
  		    }
  		
  	  public static int floorSqrt(int x) 
  	    { 
  	        // Base Cases 
  	        if (x == 0 || x == 1) 
  	            return x; 
  	  
  	        // Do Binary Search for floor(sqrt(x)) 
  	        int start = 1, end = x, ans=0; 
  	        while (start <= end) 
  	        { 
  	            int mid = (start + end) / 2; 
  	  
  	            // If x is a perfect square 
  	            if (mid*mid == x) 
  	                return mid; 
  	  
  	            // Since we need floor, we update answer when mid*mid is 
  	            // smaller than x, and move closer to sqrt(x) 
  	            if (mid*mid < x) 
  	            { 
  	                start = mid + 1; 
  	                ans = mid; 
  	            } 
  	            else   // If mid*mid is greater than x 
  	                end = mid-1; 
  	        } 
  	        return ans; 
  	    } 
  		static int div(int n,int b) {
  			int g=-1;
  			for(int i=2;i<=Math.sqrt(n);i++) {
  				if(n%i==0&&i!=b) {
  					g=i;
  					break;
  				}
  			}
  			return g;
  		}
  	 
  	  
  	  public static long gcd(long a, long a2) 
  	    { 
  	        if (a == 0) 
  	            return a2; 
  	          
  	        return gcd(a2%a, a); 
  	    } 
  	   public static long lcm(long a, long b)  
  	  {  
  	     return (a*b)/gcd(a, b);  
  	  }  
         public static boolean isPrime(int n) {
             if (n <= 1) {
                 return false;
             }
             if (n == 2) {
                 return true;
             }
             if (n % 2 == 0) {
                 return false;
             }
             for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) {
                 if (n % i == 0) {
                     return false;
                 }
             }
             return true;
         }
  	      
         static class FastScanner {
             BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
             StringTokenizer strToken = new StringTokenizer("");
             String next() {
          	   while (!strToken.hasMoreTokens()) {
          		   try {
          			   strToken = new StringTokenizer(input.readLine()); 
          		   }  catch (IOException e) { 
          			    e.printStackTrace();
          			  } 
          	   } return strToken.nextToken(); 
          	}
             String nextLine() {
          	   String strrrrr = ""; try {
          		   strrrrr = input.readLine(); 
          	   } catch (IOException e) {
          			   e.printStackTrace(); 
          		}
          	   return strrrrr; 
             }
             int nextInt() {
          	   return Integer.parseInt(next()); 
          	}
             int[] nextIntArray(int n) {
          	   int[] a = new int[n]; 
          	   for (int i = 0; i < n; i++) a[i] = nextInt(); 
          	   return a; 
             }
             long nextLong() {
          	   return Long.parseLong(next()); 
          	}
             long[] nextLongArray(int n) {
          	   long[] a = new long[n];
          	   for (int i = 0; i < n; i++) a[i] = nextLong(); 
          	   return a; 
          	}
             double nextDouble() {
          	   return Double.parseDouble(next()); 
             }
         }
  }
  class DisjointSet {
      
      private int[] parent;
      private int[] rank;
      
      public DisjointSet(int n) {
          if (n < 0) throw new IllegalArgumentException();
          parent = new int[n];
          for(int i=0;i<n;i++) parent[i]=i;
          rank = new int[n];
      }
      
      public int find(int x) {
          if (parent[x] == x) return x;
          return parent[x] = find(parent[x]); // Path compression by halving.
      }
      
      // Return false if x, y are connected.
      public boolean union(int x, int y) {
          int rootX = find(x);
          int rootY = find(y);
          if (rootX == rootY) return false;
          
          // Make root of smaller rank point to root of larger rank.
          if (rank[rootX] < rank[rootY]) parent[rootX] = rootY;
          else if (rank[rootX] > rank[rootY]) parent[rootY] = rootX;
          else {
              parent[rootX] = rootY;
              rank[rootY]++;
          }
          
          return true;
      }
  }
  
  
  class Pair{
    int value;
    int distance;
    Pair(int value,int distance){
        this.value=value;
        this.distance=distance;
    }
    
  }
  class sortbydistance implements Comparator<Pair>{
  
  	@Override
  	public int compare(Pair o1, Pair o2) {
  		return o1.distance-o2.distance;
  	}
  	
  }
  class Solution
  {
    static int[] dijkstra(int n, ArrayList<ArrayList<ArrayList<Integer>>> adj, int S)
    {
    	 PriorityQueue<Pair>set=new PriorityQueue<>(new sortbydistance());
        int dis[]=new int[n];
        Arrays.fill(dis,Integer.MAX_VALUE);
        dis[0]=0;
        while(set.isEmpty()==false) {
        	Pair p=set.poll();
        	int u=p.value;
        	for(int i=0;i<adj.get(u).size();i++) {
        		int v=adj.get(u).get(i).get(0);
        		int wt=adj.get(u).get(i).get(1);
        		if(dis[v]>dis[u]+wt) {
        			if(dis[v]!=Integer.MAX_VALUE) {
        				set.remove(new Pair(dis[v],v));
        			}
        			dis[v]=dis[u]+wt;
        			set.add(new Pair(v,dis[v]));
        		}
        		
        	}
        }
        
        
        return dis;
    }
  }
  
  /*    Points- 
   * 
   *    1) when need max and min both use treeset if duplicates are there then use treeset<int []> and add a[1] as unique
   *    number but imp is to make new TreeSet<>((x,y)->(x[0]==y[0])?(x[1]-y[1]):(x[0]-y[0]))  in this x[0]==y[0] cond.
   *    is important 
   *    
   *    2) n!/2 is not fac[n]/2 it is modmul(fac[n],inv[2],m)
   *    
   *    3) if sorting can think of TreeSet also bro or want max and min use tree set as it has last and first function
   *    
   *    4) if dp is not optimisable then think of changing the defination of dp states
  */
  class pairs{
  	int n;int e;int s;int w;
  	pairs(int n,int e,int s,int w){
  		this.n=n;this.e=e;this.s=s;this.w=w;
  	}
  }
  
  