<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:

        @lru_cache(None)
        def area(r_beg: int, c_beg: int, r_end: int, c_end: int) -> int:

            row_mx = col_mx = -inf
            row_mn = col_mn =  inf
            
            for r, c in product(range(r_beg, r_end + 1), range(c_beg, c_end + 1)):
                if grid[r][c] == 1:
                    if r < row_mn: row_mn = r
                    if r > row_mx: row_mx = r
                    if c < col_mn: col_mn = c
                    if c > col_mx: col_mx = c

            if row_mn == inf: return 0
            return (row_mx - row_mn + 1) * (col_mx - col_mn + 1)

        def horz_tricolor(comb):

            r1, r2 = comb
            return (area(0, 0,r1, n-1) +  area(r1+1, 0, r2, n-1) + 
                                          area(r2+1, 0, m-1, n-1))
        def vert_tricolor(comb):
            c1, c2 = comb
            return(area(0, 0, m-1, c1) + area(0, c1+1, m-1, c2) +
                                           area(0, c2+1,  m-1, n-1))
        def tripatch(coord):
            r, c = coord
            top, bot, lft, rgt = (
                area(0, 0, r, n-1)  , area(r+1, 0,m-1, n-1),
                area(0, 0, m-1, c)  , area(0, c+1,  m-1, n-1))

            toplft, toprgt, botlft, botrgt = (
                area(0, 0, r, c)    , area(0, c+1, r, n-1),
                area(r+1, 0, m-1, c), area(r+1, c+1, m-1, n-1))              

            return min(top + botlft + botrgt, bot + toplft + toprgt,
                       lft + toprgt + botrgt, rgt + toplft + botlft)


        m, n = len(grid), len(grid[0])

        return min(min(map(tripatch, product(range(m), range(n))  )),
                   min(map(horz_tricolor, combinations(range(m),2))),
                   min(map(vert_tricolor, combinations(range(n),2))))
742