public class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0) return 0;
int m = matrix.length;
int n = matrix[0].length;
int maxArea = 0;
int[] height = new int[n];
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
height[j]++;
} else {
height[j] = 0;
}
while (!stack.isEmpty() && height[j] <= height[stack.peek()]) {
int top = stack.pop();
maxArea = Math.max(maxArea, height[top] * (stack.isEmpty() ? j : j - 1 - stack.peek()));
}
stack.push(j);
}
while (!stack.isEmpty()) {
int top = stack.pop();
maxArea = Math.max(maxArea, height[top] * (stack.isEmpty() ? n : n - 1 - stack.peek()));
}
}
return maxArea;
}
}