Maximal Rectangle

Solution
def maximalRectangle(matrix):
    if not matrix: return 0
    max_area = 0
    heights = [0] * len(matrix[0])

    def largestRectangleArea(heights):
        stack = [-1]
        max_area = 0
        for i in range(len(heights)):
            while stack[-1] != -1 and heights[stack[-1]] >= heights[i]:
                current_height = heights[stack.pop()]
                current_width = i - stack[-1] - 1
                max_area = max(max_area, current_height * current_width)
            stack.append(i)
        while stack[-1] != -1:
            current_height = heights[stack.pop()]
            current_width = len(heights) - stack[-1] - 1
            max_area = max(max_area, current_height * current_width)
        return max_area

    for row in matrix:
        for i in range(len(row)):
            if row[i] == '1': heights[i] += 1
            else: heights[i] = 0
        max_area = max(max_area, largestRectangleArea(heights))
    return max_area

Time Complexity

O(R C)

Space Complexity

O(C)