<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p>

<p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p>

<p><strong>Note</strong> that the rectangles are allowed to touch.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">5</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p>

<ul>
	<li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
	<li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li>
	<li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">5</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p>

<ul>
	<li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li>
	<li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li>
	<li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li>
</ul>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either 0 or 1.</li>
	<li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li>
</ul>

class Solution:
    def minimumSum(self, grid):

        @lru_cache(None)
        def minimumArea(min_row, max_row, min_col, max_col):
            min_r, max_r = inf, -inf
            min_c, max_c = inf, -inf
            
            for i in range(min_row, max_row):
                for j in range(min_col, max_col):
                    if grid[i][j] == 1:
                        min_r, max_r = min(min_r, i), max(max_r, i)
                        min_c, max_c = min(min_c, j), max(max_c, j)
            
            if min_r == inf or min_c == inf:return 0
 
            return (max_r - min_r + 1) * (max_c - min_c + 1)

        n = len(grid)
        m = len(grid[0])
        result = inf
        
        for end_row in range(1, n):
            for end_col in range(1, m):
                # First rectangle
                first_area = minimumArea(0, end_row, 0, end_col)
                
                # Considering next combinations for the other two rectangles
                top_area = minimumArea(end_row, n, 0, m)
                right_area = minimumArea(0, end_row, end_col, m)
                
                bottom_area = minimumArea(end_row, n, 0, end_col)
                left_area = minimumArea(0, n, end_col, m)
                
                # Two possible combinations of the remaining rectangles
                second_area = min(top_area + right_area, bottom_area + left_area)
                
                result = min(result, first_area + second_area)
                
        # Consider parallel splits along the x-axis and y-axis
        for x1 in range(1, n-1):
            for x2 in range(x1+1, n):
                top_area = minimumArea(0, x1, 0, m)
                middle_area = minimumArea(x1, x2, 0, m)
                bottom_area = minimumArea(x2, n, 0, m)
                result = min(result, top_area + middle_area + bottom_area)
        
        for y1 in range(1, m-1):
            for y2 in range(y1+1, m):
                left_area = minimumArea(0, n, 0, y1)
                middle_area = minimumArea(0, n, y1, y2)
                right_area = minimumArea(0, n, y2, m)
                result = min(result, left_area + middle_area + right_area)
        
        # Case when one rectangle is [0:end_row][:] and other two rectangles split horizontally
        for end_row in range(1, n-1):
            for split_col in range(1, m):
                top_area = minimumArea(0, end_row, 0, m)
                bottom_left_area = minimumArea(end_row, n, 0, split_col)
                bottom_right_area = minimumArea(end_row, n, split_col, m)
                result = min(result, top_area + bottom_left_area + bottom_right_area)
        
        # Case when one rectangle is [end_row:n][:] and other two rectangles split horizontally 
        for end_row in range(1, n-1):
            for split_col in range(1, m):
                bottom_area = minimumArea(end_row, n, 0, m)
                top_left_area = minimumArea(0, end_row, 0, split_col)
                top_right_area = minimumArea(0, end_row, split_col, m)
                result = min(result, bottom_area + top_left_area + top_right_area)
        
        # Case when one rectangle is [:][0:end_col] and other two rectangles split vertically 
        for end_col in range(1, m-1):
            for split_row in range(1, n):
                left_area = minimumArea(0, n, 0, end_col)
                top_right_area = minimumArea(0, split_row, end_col, m)
                bottom_right_area = minimumArea(split_row, n, end_col, m)
                result = min(result, left_area + top_right_area + bottom_right_area)
        
        # Case when one rectangle is [:][end_col:m] and other two rectangles split vertically 
        for end_col in range(1, m-1):
            for split_row in range(1, n):
                right_area = minimumArea(0, n, end_col, m)
                bottom_left_area = minimumArea(split_row, n, 0, end_col)
                top_left_area = minimumArea(0, split_row, 0, end_col)
                result = min(result, right_area + bottom_left_area + top_left_area)
        
        return result

1313