Python monotone stack solution with comments and explanation.
This problem can be visualised as an extension to largest rectangle in histogram\nIn the above problem you can use Next Smallest Element (NSE) logic on a monoto
This problem can be visualised as an extension to [largest rectangle in histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)\nIn the above problem you can use Next Smallest Element (NSE) logic on a monotonically increasing stack to find the maximum rectangle possible in the histogram given.\n\nYou can easily extend that concept on this 2D matrix by constructing/updating histogram row by row and processing on the histogram. \n1.  For the first row the whole row is a histogram with bar heights either 1 or 0\n2.  For the next row we update the histogram as\n        =>  If the value of current grid is \'0\' then histogram height is reset to 0 (as the continuation of the rectangle breaks here)\n\t\t=>  If the value of the current grid is \'1\' then histogram height can be increased by 1 vertically\n3.  So on for rest of the rows...\n**Note:**  As we are processing row by row so rectangle continuity in horizontal direction is handled in histogram automatically while the vertical continuity is handled by updating the histogram as in step 2.\n4. Now after updating the histogram for each row, we calculate the maximum area of rectangle possible.\n5. Once we have maximum from all the rows, we pick the maximum of the maximums to get our final answer. \n\nExample:\n**Input:** \n```\n[\n  ["1","0","1","0","0"],\n  ["1","0","1","1","1"],\n  ["1","1","1","1","1"],\n  ["1","0","0","1","0"]\n]\n```\n\n**Histogram creation and updation for each row of matrix:**\n```\nRow 1 => [1, 0, 1, 0, 0]   [current max = 1 and overall max = 1]\nRow 2 => [2, 0, 2, 1, 1]   [current max = 3 and overall max = 3]\nRow 3 => [3, 1, 3, 2, 2]   [current max = 6 and overall max = 6]\nRow 4 => [4, 0, 0, 3, 0]   [current max = 4 and overall max = 6]\n```\nAs you can see the final answer will be picked as 6\n\n**Code:**\n```\nclass Solution:\n    def maximalRectangle(self, matrix: List[List[str]]) -> int:\n        if not matrix:\n            return 0  # To handle `matrix = []` case\n\t\t\n\t\t# Prepare histogram for the first row of input matrix and find maximum area.\n        histogram = list(map(int, matrix[0]))\n        histogram.append(0)\n        current_max = self.max_rect_histogram(histogram)\n\t\t\n\t\t# Process on the remaining rows of matrix\n        for row in matrix[1:]:\n            for i in range(len(row)):\n\t\t\t    # update histogram, if grid is a \'0\' reset histogram height else increase height by one\n                if row[i] == \'0\':\n                    histogram[i] = 0\n                else:\n                    histogram[i] += int(row[i])\n\t\t\t# Once histogram is updated for current row find the maximum rectangle area in current histogram\n            current_max = max(current_max, self.max_rect_histogram(histogram))\n        return current_max\n        \n    def max_rect_histogram(self, histogram):\n        stack = [-1]\n        mx = 0\n        for i,v in enumerate(histogram):\n\t\t\t# As long as the stack has increasing value keep adding the index of histogram\n\t\t\t# If the insertion to stack will result in a decreasing stack, then keep poping till it becomes increasing again\n\t\t\t# For each pop, calculate the area of the rectangle\n            while(stack[-1] != -1 and histogram[stack[-1]] > v):\n                height = histogram[stack.pop()]\n\t\t\t\t# i is right limit, stack[-1] is left limit so width of rectangle in consideration is r-l-1\n                width = i - stack[-1] - 1\n                mx = max(mx, height*width)\n            stack.append(i)\n        return mx\n```
[]