<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:
        n, m = len(grid), len(grid[0])

        @cache
        def lrud(row1, row2, col1, col2):
            l, r, u, d = col2, col1, row2, row1
            for i in range(row1, row2+1):
                for j in range(col1, col2+1):
                    if grid[i][j] == 1:
                        l, r, u, d = min(l, j), max(r, j), min(u, i), max(d, i)
            return l, r, u, d

        def split(row1, row2, col1, col2):
            return min(split_horizontal(row1, row2, col1, col2), split_vertical(row1, row2, col1, col2))
        
        def split_horizontal(row1, row2, col1, col2):
            # print('horizontal: ', row1, row2, col1, col2)
            v = inf
            for s in range(row1, row2):
                l1, r1, u1, d1 = lrud(row1, s, col1, col2)
                l2, r2, u2, d2 = lrud(s+1, row2, col1, col2)
                if l1 > r1 or u1 > d1 or l2 > r2 or u2 > d2:
                    continue
                # print('horizontal: ', s, l1, r1, u1, d1, l2, r2, u2, d2)
                v = min(v, (r1 - l1 + 1) * (d1 - u1 + 1) + (r2 - l2 + 1) * (d2 - u2 + 1))
            return v
        
        def split_vertical(row1, row2, col1, col2):
            # print('vertical: ', row1, row2, col1, col2)
            v = inf
            for s in range(col1, col2):
                l1, r1, u1, d1 = lrud(row1, row2, col1, s)
                l2, r2, u2, d2 = lrud(row1, row2, s+1, col2)
                if l1 > r1 or u1 > d1 or l2 > r2 or u2 > d2:
                    continue
                # print('vertical: ', s, l1, r1, u1, d1, l2, r2, u2, d2)
                v = min(v, (d1 - u1 + 1) * (r1 - l1 + 1) + (d2 - u2 + 1) * (r2 - l2 + 1))
            return v
        
        ans = n * m
        for s in range(n-1):
            l1, r1, u1, d1 = lrud(0, s, 0, m-1)
            if l1 > r1 or u1 > d1:
                continue
            # print('H:', s, l1, r1, u1, d1, split(s+1, n-1, 0, m-1))
            ans = min(ans, (r1 - l1 + 1) * (d1 - u1 + 1) + split(s+1, n-1, 0, m-1))
        for s in range(1, n):
            l1, r1, u1, d1 = lrud(s, n-1, 0, m-1)
            if l1 > r1 or u1 > d1:
                continue
            # print('H2:', s, l1, r1, u1, d1, split(0, s-1, 0, m-1))
            ans = min(ans, (r1 - l1 + 1) * (d1 - u1 + 1) + split(0, s-1, 0, m-1))
        for s in range(m-1):
            l1, r1, u1, d1 = lrud(0, n-1, 0, s)
            if l1 > r1 or u1 > d1:
                continue
            # print('V:', s, l1, r1, u1, d1, split(0, n-1, s+1, m-1))
            ans = min(ans, (d1 - u1 + 1) * (r1 - l1 + 1) + split(0, n-1, s+1, m-1))
        for s in range(1, m):
            l1, r1, u1, d1 = lrud(0, n-1, s, m-1)
            if l1 > r1 or u1 > d1:
                continue
            # print('V2:', s, l1, r1, u1, d1, split(0, n-1, 0, s-1))
            ans = min(ans, (d1 - u1 + 1) * (r1 - l1 + 1) + split(0, n-1, 0, s-1))
        return ans
17700