publicintmaximalRectangle(boolean[][] matrix) {if (matrix ==null||matrix.length==0|| matrix[0].length==0) {return0; }int m =matrix.length;int n = matrix[0].length;int[] curRowHis =newint[n];int max =Integer.MIN_VALUE;// T: O(mn)for (int i =0; i < m; i++) {for (int j =0; j < n; j++) {if (!matrix[i][j]) { curRowHis[j] =0; } else { curRowHis[j] +=1; } }int curMax =histogram(curRowHis); max =Math.max(curMax, max); }return max;}privateinthistogram(int[] A) {int max =Integer.MIN_VALUE;Stack<Integer> stack =newStack<>();for (int i =0; i <=A.length; i++) {int cur = (i ==A.length) ?-1:A[i];while (!stack.isEmpty() && cur <=A[stack.peek()]) {int h =A[stack.pop()];int w =stack.isEmpty() ? i : i -stack.peek() -1;int area = h * w; max =Math.max(max, area); }stack.push(i); }return max; }