<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, mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])
        acc = [[0] * (n + 1) for _ in range(m + 1)]
        for i in range(m):
            for j in range(n):
                acc[i][j] = mat[i][j] + acc[i][j - 1]
            for j in range(n):
                acc[i][j] += acc[i - 1][j]
                
        def area(i0, i1, j0, j1):
            if acc[i0 - 1][j0 - 1] == acc[i1][j1]:
                return 0
            mir = inf
            mxr = 0
            mic = inf
            mxc = 0
            for i in range(i0, i1 + 1):
                for j in range(j0, j1 + 1):
                    if mat[i][j]:
                        mir = min(i, mir)
                        mxr = max(i, mxr)
                        mic = min(j, mic)
                        mxc = max(j, mxc)
            return (mxc - mic + 1) * (mxr - mir + 1)
        
        ans = inf
        
        # 2 hcuts
        for i in range(m - 2):
            a1 = area(0, i, 0, n - 1)
            if a1 and a1 < ans:
                for ii in range(i + 1, m - 1):
                    a2 = area(i + 1, ii, 0, n - 1)
                    if a2 and a1 + a2 < ans:
                        a3 = area(ii + 1, m - 1, 0, n - 1)
                        if a3:
                            ans = min(ans, a1 + a2 + a3)
        for i in range(m - 1):
            # hcut, take top
            atop = area(0, i, 0, n - 1)
            if atop and atop < ans:
                # bottom 2 vcuts 
                for j in range(n - 1):
                    aleft = area(i + 1, m - 1, 0, j)
                    if aleft and atop + aleft < ans:
                        aright = area(i + 1, m - 1, j + 1, n - 1)
                        if aright:
                            ans = min(ans, atop + aleft + aright)
            # hcut, take bottom
            abot = area(i + 1, m - 1, 0, n - 1)
            if abot and abot < ans:
                # top 2 vcuts 
                for j in range(n - 1):
                    aleft = area(0, i, 0, j)
                    if aleft and abot + aleft < ans:
                        aright = area(0, i, j + 1, n - 1)
                        if aright:
                            ans = min(ans, abot + aleft + aright)
        
        # 2 vcuts
        for j in range(n - 2):
            a1 = area(0, m - 1, 0, j)
            if a1 and a1 < ans:
                for jj in range(j + 1, n - 1):
                    a2 = area(0, m - 1, j + 1, jj)
                    if a2 and a1 + a2 < ans:
                        a3 = area(0, m - 1, jj + 1, n - 1)
                        if a3:
                            ans = min(ans, a1 + a2 + a3)
        
        for j in range(n - 1):
            # vcut, take left
            aleft = area(0, m - 1, 0, j)
            if aleft and aleft < ans:
                # right, 2 hcuts
                for i in range(m - 1):
                    atop = area(0, i, j + 1, n - 1)
                    if atop and aleft + atop < ans:
                        abot = area(i + 1, m - 1, j + 1, n - 1)
                        if abot:
                            ans = min(ans, aleft + atop + abot)
            # hcut, take right
            aright = area(0, m - 1, j + 1, n - 1)
            if aright and aright < ans:
                # left, 2 vcuts
                for i in range(m - 1):
                    atop = area(0, i, 0, j)
                    if atop and aright + atop < ans:
                        abot = area(i + 1, m - 1, 0, j)
                        if abot:
                            ans = min(ans, aright + atop + abot)
        
        return ans
2648