public int[][] normalizeMatrix(int[][] matrix){
  long[] rowMap=new long[matrix.length];
  for (int i=0; i < matrix.length; i++) {
    if (matrix[i].length <= 1)     continue;
    int count=matrix[i][0];
    if (count <= 0)     continue;
    rowMap[i]=(long)count << 32 | i;
  }
  Arrays.sort(rowMap);
  int[][] newMatrix=new int[matrix.length][];
  int prevCount=-1;
  int fillp1=0;
  int fillp2=0;
  for (int i=0; ; i++) {
    int[] row;
    if (i < matrix.length) {
      long rowMapEntry=rowMap[rowMap.length - i - 1];
      if (rowMapEntry == 0)       continue;
      row=matrix[(int)rowMapEntry];
      assert (rowMapEntry >>> 32 == row[0]);
    }
 else {
      row=new int[]{-1};
    }
    if (row[0] != prevCount && fillp2 > fillp1) {
      int length=0;
      for (int p=fillp1; p < fillp2; p++) {
        int[] row0=newMatrix[p];
        assert (row0[0] == prevCount);
        length+=row0.length - 1;
      }
      int[] row1=new int[1 + length];
      row1[0]=prevCount;
      int rfillp=1;
      for (int p=fillp1; p < fillp2; p++) {
        int[] row0=newMatrix[p];
        assert (row0[0] == prevCount);
        System.arraycopy(row0,1,row1,rfillp,row0.length - 1);
        rfillp+=row0.length - 1;
      }
      if (!isSorted(row1,1,true)) {
        Arrays.sort(row1,1,row1.length);
        int jfillp=2;
        for (int j=2; j < row1.length; j++) {
          if (row1[j] != row1[j - 1])           row1[jfillp++]=row1[j];
        }
        if (jfillp < row1.length) {
          int[] newRow1=new int[jfillp];
          System.arraycopy(row1,0,newRow1,0,jfillp);
          row1=newRow1;
        }
      }
      newMatrix[fillp1++]=row1;
      fillp2=fillp1;
    }
    if (i == matrix.length)     break;
    prevCount=row[0];
    newMatrix[fillp2++]=row;
  }
  assert (fillp1 == fillp2);
  matrix=newMatrix;
  if (fillp1 < matrix.length) {
    newMatrix=new int[fillp1][];
    System.arraycopy(matrix,0,newMatrix,0,fillp1);
    matrix=newMatrix;
  }
  return matrix;
}

public int [ ] [ ] normalizeMatrix ( int [ ] [ ] matrix ) { long [ ] rowMap = new long [ matrix . length ] ; for ( int i = 0 ; i < matrix . length ; i ++ ) { if ( matrix [ i ] . length <= 1 ) continue ; int count = matrix [ i ] [ 0 ] ; if ( count <= 0 ) continue ; rowMap [ i ] = ( long ) count << 32 | i ; } Arrays . sort ( rowMap ) ; int [ ] [ ] newMatrix = new int [ matrix . length ] [ ] ; int prevCount = - 1 ; int fillp1 = 0 ; int fillp2 = 0 ; for ( int i = 0 ; ; i ++ ) { int [ ] row ; if ( i < matrix . length ) { long rowMapEntry = rowMap [ rowMap . length - i - 1 ] ; if ( rowMapEntry == 0 ) continue ; row = matrix [ ( int ) rowMapEntry ] ; assert ( rowMapEntry > > > 32 == row [ 0 ] ) ; } else { row = new int [ ] { - 1 } ; } if ( row [ 0 ] != prevCount && fillp2 > fillp1 ) { int length = 0 ; for ( int p = fillp1 ; p < fillp2 ; p ++ ) { int [ ] row0 = newMatrix [ p ] ; assert ( row0 [ 0 ] == prevCount ) ; length += row0 . length - 1 ; } int [ ] row1 = new int [ 1 + length ] ; row1 [ 0 ] = prevCount ; int rfillp = 1 ; for ( int p = fillp1 ; p < fillp2 ; p ++ ) { int [ ] row0 = newMatrix [ p ] ; assert ( row0 [ 0 ] == prevCount ) ; System . arraycopy ( row0 , 1 , row1 , rfillp , row0 . length - 1 ) ; rfillp += row0 . length - 1 ; } if ( ! isSorted ( row1 , 1 , true ) ) { Arrays . sort ( row1 , 1 , row1 . length ) ; int jfillp = 2 ; for ( int j = 2 ; j < row1 . length ; j ++ ) { if ( row1 [ j ] != row1 [ j - 1 ] ) row1 [ jfillp ++ ] = row1 [ j ] ; } if ( jfillp < row1 . length ) { int [ ] newRow1 = new int [ jfillp ] ; System . arraycopy ( row1 , 0 , newRow1 , 0 , jfillp ) ; row1 = newRow1 ; } } newMatrix [ fillp1 ++ ] = row1 ; fillp2 = fillp1 ; } if ( i == matrix . length ) break ; prevCount = row [ 0 ] ; newMatrix [ fillp2 ++ ] = row ; } assert ( fillp1 == fillp2 ) ; matrix = newMatrix ; if ( fillp1 < matrix . length ) { newMatrix = new int [ fillp1 ] [ ] ; System . arraycopy ( matrix , 0 , newMatrix , 0 , fillp1 ) ; matrix = newMatrix ; } return matrix ; }
sort rows and columns . merge adjacent rows with the same key element [ 0 ] . make a fresh copy of all of it .
