int find_max_matrix(int A[ROW][COL])
    {
     int i, j;
     int max, cur_max;
     cur_max = 0;

     //Calculate Auxilary matrix
     for (i=1; i<ROW; i++)
         for(j=0; j<COL; j++)
         {
             if(A[i][j] == 1)
                 A[i][j] = A[i-1][j] + 1;
         }

     //Calculate maximum area in S for each row
     for (i=0; i<ROW; i++)
     {
         max = largestArea(A[i], COL);
         if (max > cur_max)
             cur_max = max;
     }

     //Regenerate Oriignal matrix
     for (i=ROW-1; i>0; i--)
         for(j=0; j<COL; j++)
         {
             if(A[i][j])
                 A[i][j] = A[i][j] - A[i-1][j];
         }

     return cur_max;
    }
 int largestArea(vector<int>& heights, int start, int end)
 {
     if (start > end)
         return 0;
     if (start == end)
         return heights[start];
     int minHeight = heights[start];
     int minIndex = start;
     bool sorted = true;
     for (int i = start+1; i <= end; ++i)
     {
         if (heights[i] < heights[i-1])
             sorted = false;
         if (minHeight > heights[i])
         {
             minHeight = heights[i];
             minIndex = i;
         }
     }
     if (sorted)
     {
         int _max = 0;
         for (int i = start; i <= end; i++) {
             _max = max(_max, heights[i] * (end - i + 1));
         }
         return _max;
     }
     int maxArea = (end-start+1) * minHeight;
     int leftIndex = minIndex-1;
     while (start <= leftIndex && heights[leftIndex] == heights[minIndex])
         leftIndex--;
     int rightIndex = minIndex+1;
     while (rightIndex <= end && heights[rightIndex] == heights[minIndex])
         rightIndex++;
     int leftArea = largestArea(heights, start, leftIndex);
     int rightArea = largestArea(heights, rightIndex, end);
     maxArea = max(maxArea, leftArea);
     maxArea = max(maxArea, rightArea);
     return maxArea;
 }
 /*
  * Method1: O(n*logn) complecity
  */
 int largestRectangleArea(vector<int>& heights) {
     if (heights.size() == 0)
         return 0;
     int area = largestArea(heights, 0, heights.size()-1);
     return area;
 }