<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: List[List[int]]) -> int:
        def get_minimum_area_in_range(top_left_r, top_left_c, bottom_right_r, bottom_right_c):
            non_zero_top_left_r, non_zero_top_left_c = inf, inf
            non_zero_bottom_right_r, non_zero_bottom_right_c = -inf, -inf

            for i in range(top_left_r, bottom_right_r + 1):
                for j in range(top_left_c, bottom_right_c + 1):
                    if grid[i][j] == 1:
                        non_zero_top_left_r = min(non_zero_top_left_r, i)
                        non_zero_top_left_c = min(non_zero_top_left_c, j)
                        non_zero_bottom_right_r = max(non_zero_bottom_right_r, i)
                        non_zero_bottom_right_c = max(non_zero_bottom_right_c, j)

            return (non_zero_bottom_right_r - non_zero_top_left_r + 1) * (non_zero_bottom_right_c - non_zero_top_left_c + 1)

        row_count, col_count = len(grid), len(grid[0])
        minimum_area_sum = inf

        # 1. [ ]
        #    [ ]
        #    [ ]
        for first_cut in range(row_count - 2):
            for second_cut in range(first_cut + 1, row_count - 1):
                area1 = get_minimum_area_in_range(0, 0, first_cut, col_count - 1)
                area2 = get_minimum_area_in_range(first_cut + 1, 0, second_cut, col_count - 1)
                area3 = get_minimum_area_in_range(second_cut + 1, 0, row_count - 1, col_count - 1)
                minimum_area_sum = min(minimum_area_sum, area1 + area2 + area3)

        # 2. [ ][ ][ ]
        for first_cut in range(col_count - 2):
            for second_cut in range(first_cut + 1, col_count - 1):
                area1 = get_minimum_area_in_range(0, 0, row_count - 1, first_cut)
                area2 = get_minimum_area_in_range(0, first_cut + 1, row_count - 1, second_cut)
                area3 = get_minimum_area_in_range(0, second_cut + 1, row_count - 1, col_count - 1)
                minimum_area_sum = min(minimum_area_sum, area1 + area2 + area3)

        # 3. [ ][ ]
        #    [    ]
        for row_cut in range(row_count - 1):
            for col_cut in range(col_count - 1):
                area1 = get_minimum_area_in_range(0, 0, row_cut, col_cut)
                area2 = get_minimum_area_in_range(0, col_cut + 1, row_cut, col_count - 1)
                area3 = get_minimum_area_in_range(row_cut + 1, 0, row_count - 1, col_count - 1)
                minimum_area_sum = min(minimum_area_sum, area1 + area2 + area3)

        # 4. [    ]
        #    [ ][ ]
        for row_cut in range(row_count - 1):
            for col_cut in range(col_count - 1):
                area1 = get_minimum_area_in_range(0, 0, row_cut, col_count - 1)
                area2 = get_minimum_area_in_range(row_cut + 1, 0, row_count - 1, col_cut)
                area3 = get_minimum_area_in_range(row_cut + 1, col_cut + 1, row_count - 1, col_count - 1)
                minimum_area_sum = min(minimum_area_sum, area1 + area2 + area3)

        # 5. []| |
        #    []| |
        for row_cut in range(row_count - 1):
            for col_cut in range(col_count - 1):
                area1 = get_minimum_area_in_range(0, 0, row_cut, col_cut)
                area2 = get_minimum_area_in_range(row_cut + 1, 0, row_count - 1, col_cut)
                area3 = get_minimum_area_in_range(0, col_cut + 1, row_count - 1, col_count - 1)
                minimum_area_sum = min(minimum_area_sum, area1 + area2 + area3)

        # 6. | |[]
        #    | |[]
        for row_cut in range(row_count - 1):
            for col_cut in range(col_count - 1):
                area1 = get_minimum_area_in_range(0, 0, row_count - 1, col_cut)
                area2 = get_minimum_area_in_range(0, col_cut + 1, row_cut, col_count - 1)
                area3 = get_minimum_area_in_range(row_cut + 1, col_cut + 1, row_count - 1, col_count - 1)
                minimum_area_sum = min(minimum_area_sum, area1 + area2 + area3)

        return minimum_area_sum
3887