<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:
        m, n = len(grid), len(grid[0])
        res = m*n
        area = lambda tup: (tup[2]-tup[0]+1) * (tup[3]-tup[1]+1) if tup[0] < inf else inf

        # DP stores (minX, minY, maxX, maxY) of every block

        # horizontal slices
        hor = [[(inf,inf,-inf,-inf)]*m for _ in range(m)]
        for i in range(m):
            if any(grid[i]):
                hor[i][i] = (i, grid[i].index(1), \
                            i,  n-1-grid[i][::-1].index(1))
                for ii in range(i):
                    hor[ii][i] =    (min(hor[ii][i-1][0], i), \
                                    min(hor[ii][i-1][1], hor[i][i][1]), \
                                    i, 
                                    max(hor[ii][i-1][3], hor[i][i][3]))
            else:
                for ii in range(i):
                    hor[ii][i] = hor[ii][i-1]

        # vertical slices
        ver = [[(inf,inf,-inf,-inf)]*n for _ in range(n)]
        tp = list(zip(*grid))
        for j in range(n):
            if any(tp[j]):
                ver[j][j] = (tp[j].index(1), j, \
                            m-1-tp[j][::-1].index(1), j)
                for jj in range(j):
                    ver[jj][j] =    (min(ver[jj][j-1][0], ver[j][j][0]), \
                                    min(ver[jj][j-1][1],j), \
                                    max(ver[jj][j-1][2], ver[j][j][2]), \
                                    j)
            else:
                for jj in range(j):
                    ver[jj][j] = ver[jj][j-1]
        
        # top-left blocks
        tl = [[(inf,inf,-inf,-inf)]*(n+1) for _ in range(m+1)]
        for i in range(m):
            for j in range(n):
                if grid[i][j]:
                    tl[i][j] =  (min(tl[i-1][j][0], tl[i][j-1][0], i), \
                                min(tl[i-1][j][1], tl[i][j-1][1], j), 
                                i, \
                                j)
                else:
                    tl[i][j] =  (min(tl[i-1][j][0], tl[i][j-1][0]), \
                                min(tl[i-1][j][1], tl[i][j-1][1]), \
                                max(tl[i-1][j][2], tl[i][j-1][2]), \
                                max(tl[i-1][j][3], tl[i][j-1][3]))

        # top-right blocks
        tr = [[(inf,inf,-inf,-inf)]*(n+1) for _ in range(m+1)]
        for i in range(m):
            for j in range(n-1,-1,-1):
                if grid[i][j]:
                    tr[i][j] =  (min(tr[i-1][j][0], tr[i][j+1][0], i), \
                                j, \
                                i, \
                                max(tr[i-1][j][3], tr[i][j+1][3], j))
                else:
                    tr[i][j] =  (min(tr[i-1][j][0], tr[i][j+1][0]), \
                                min(tr[i-1][j][1], tr[i][j+1][1]), \
                                max(tr[i-1][j][2], tr[i][j+1][2]), \
                                max(tr[i-1][j][3], tr[i][j+1][3]))

        # bottom-left blocks
        bl = [[(inf,inf,-inf,-inf)]*(n+1) for _ in range(m+1)]
        for i in range(m-1,-1,-1):
            for j in range(n):
                if grid[i][j]:
                    bl[i][j] =  (i, \
                                min(bl[i+1][j][1], bl[i][j-1][1], j), \
                                max(bl[i+1][j][2], bl[i][j-1][2], i), \
                                j)
                else:
                    bl[i][j] =  (min(bl[i+1][j][0], bl[i][j-1][0]), \
                                min(bl[i+1][j][1], bl[i][j-1][1]), \
                                max(bl[i+1][j][2], bl[i][j-1][2]), \
                                max(bl[i+1][j][3], bl[i][j-1][3]))

        # bottom-right blocks
        br = [[(inf,inf,-inf,-inf)]*(n+1) for _ in range(m+1)]
        for i in range(m-1,-1,-1):
            for j in range(n-1,-1,-1):
                if grid[i][j]:
                    br[i][j] =  (i, \
                                j, \
                                max(br[i+1][j][2], br[i][j+1][2], i), \
                                max(br[i+1][j][3], br[i][j+1][3], j))
                else:
                    br[i][j] =  (min(br[i+1][j][0], br[i][j+1][0]), \
                                min(br[i+1][j][1], br[i][j+1][1]), \
                                max(br[i+1][j][2], br[i][j+1][2]), \
                                max(br[i+1][j][3], br[i][j+1][3]))

        # The 6 cases

        for i in range(1,m):

            # case 1: Three horizontal slices
            for j in range(i+1,m):
                res = min(res, area(hor[0][i-1]) + area(hor[i][j-1]) + area(hor[j][m-1]))

            # case 2: Top + Bottom-left + Bottom-right
            for j in range(1,n):
                res = min(res, area(hor[0][i-1]) + area(bl[i][j-1]) + area(br[i][j]))

            # case 3: Bottom + Top-left + Top-right
            for j in range(1,n):
                res = min(res, area(hor[i][m-1]) + area(tl[i-1][j-1]) + area(tr[i-1][j]))

        # The other 3 cases (symmetrical)

        for i in range(1,n):

           # case 4: Three vertical slices
            for j in range(i+1,n):
                res = min(res, area(ver[0][i-1]) + area(ver[i][j-1]) + area(ver[j][n-1]))

            # case 5: Left + Top-right + Bottom-right
            for j in range(1,m):
                res = min(res, area(ver[0][i-1]) + area(tr[j-1][i]) + area(br[j][i]))

            # case 6: Right + Top-left + Bottom-left
            for j in range(1,m):
                res = min(res, area(ver[i][n-1]) + area(tl[j-1][i-1]) + area(bl[j][i-1]))
                    
        return res
265