<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])
        inds = []  # grid of inds at each row
        for arr in grid:
            a1 = []
            for i, val in enumerate(arr):
                if val == 1:
                    a1.append(i)
            inds.append(a1)
        colInds = [[] for j in range(n)]
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    colInds[j].append(i)
        # print(colInds)

        def getLR(i):
            if len(inds[i]):
                return [inds[i][0], inds[i][-1]]
            return [float("inf"), float("-inf")]

        def getTB(j):
            if len(colInds[j]):
                return [colInds[j][0], colInds[j][-1]]
            return [float("inf"), float("-inf")]

        @lru_cache(maxsize=None)
        def getFromBottom(i):
            if i == m - 1:
                return getLR(i)
            l1, r1 = getFromBottom(i + 1)
            l2, r2 = getLR(i)
            return [min(l1, l2), max(r1, r2)]

        @lru_cache(maxsize=None)
        def getFromRight(j):
            if j == n - 1:
                return getTB(j)
            t1, b1 = getFromRight(j + 1)
            t2, b2 = getTB(j)
            return [min(t1, t2), max(b1, b2)]

        @lru_cache(maxsize=None)
        def getFromTop(i):
            if i == 0:
                return getLR(i)
            l1, r1 = getFromTop(i - 1)
            l2, r2 = getLR(i)
            return [min(l1, l2), max(r1, r2)]

        @lru_cache(maxsize=None)
        def getFromLeft(j):
            if j == 0:
                return getTB(j)
            t1, b1 = getFromLeft(j - 1)
            t2, b2 = getTB(j)
            return [min(t1, t2), max(b1, b2)]

        res = m * n

        @lru_cache(maxsize=None)
        def getFromTL(i, j):
            l, r, t, b = float("inf"), float("-inf"), float("inf"), float("-inf")
            l3, r3, t3, b3 = float("inf"), float("-inf"), float("inf"), float("-inf")
            if grid[i][j] == 1:
                l = r = j
                t = b = i
            if i == 0 and j == 0:
                return [l, r, t, b]
            if i == 0:
                l2, r2, t2, b2 = getFromTL(i, j - 1)
            elif j == 0:
                l2, r2, t2, b2 = getFromTL(i - 1, j)
            else:
                l2, r2, t2, b2 = getFromTL(i, j - 1)
                l3, r3, t3, b3 = getFromTL(i - 1, j)
            return [min(l, l2, l3), max(r, r2, r3), min(t, t2, t3), max(b, b2, b3)]

        @lru_cache(maxsize=None)
        def getFromTR(i, j):
            l, r, t, b = float("inf"), float("-inf"), float("inf"), float("-inf")
            l3, r3, t3, b3 = float("inf"), float("-inf"), float("inf"), float("-inf")
            if grid[i][j] == 1:
                l = r = j
                t = b = i
            if i == 0 and j == n - 1:
                return [l, r, t, b]
            if i == 0:
                l2, r2, t2, b2 = getFromTR(i, j + 1)
            elif j == n - 1:
                l2, r2, t2, b2 = getFromTR(i - 1, j)
            else:
                l2, r2, t2, b2 = getFromTR(i, j + 1)
                l3, r3, t3, b3 = getFromTR(i - 1, j)
            return [min(l, l2, l3), max(r, r2, r3), min(t, t2, t3), max(b, b2, b3)]

        @lru_cache(maxsize=None)
        def getFromBL(i, j):
            l, r, t, b = float("inf"), float("-inf"), float("inf"), float("-inf")
            l3, r3, t3, b3 = float("inf"), float("-inf"), float("inf"), float("-inf")
            if grid[i][j] == 1:
                l = r = j
                t = b = i
            if i == m - 1 and j == 0:
                return [l, r, t, b]
            if i == m - 1:
                l2, r2, t2, b2 = getFromBL(i, j - 1)
            elif j == 0:
                l2, r2, t2, b2 = getFromBL(i + 1, j)
            else:
                l2, r2, t2, b2 = getFromBL(i, j - 1)
                l3, r3, t3, b3 = getFromBL(i + 1, j)
            return [min(l, l2, l3), max(r, r2, r3), min(t, t2, t3), max(b, b2, b3)]

        @lru_cache(maxsize=None)
        def getFromBR(i, j):
            l, r, t, b = float("inf"), float("-inf"), float("inf"), float("-inf")
            l3, r3, t3, b3 = float("inf"), float("-inf"), float("inf"), float("-inf")
            if grid[i][j] == 1:
                l = r = j
                t = b = i
            if i == m - 1 and j == n - 1:
                return [l, r, t, b]
            if i == m - 1:
                l2, r2, t2, b2 = getFromBR(i, j + 1)
            elif j == n - 1:
                l2, r2, t2, b2 = getFromBR(i + 1, j)
            else:
                l2, r2, t2, b2 = getFromBR(i, j + 1)
                l3, r3, t3, b3 = getFromBR(i + 1, j)
            return [min(l, l2, l3), max(r, r2, r3), min(t, t2, t3), max(b, b2, b3)]

        def getArea(coord):
            l, r, t, b = coord
            if l == float("inf"):
                return 1
            return (r - l + 1) * (b - t + 1)

        if m > 2:
            sf1 = getLR(0)
            for a in range(m - 2):
                l1, r1 = getLR(a)
                l2, r2 = sf1
                sf1 = [min(l1, l2), max(r1, r2)]
                area1 = getArea(getFromTL(a, n - 1))
                sf2 = getLR(a + 1)
                for b in range(a + 1, m - 1):
                    l1, r1 = getLR(b)
                    l2, r2 = sf2
                    sf2 = [min(l1, l2), max(r1, r2)]
                    area2 = 1
                    if sf2[0] < n:
                        area2 = (b - a) * (sf2[1] - sf2[0] + 1)
                    area3 = getArea(getFromBL(b + 1, n - 1))
                    res = min(res, area1 + area2 + area3)
        if n > 2:
            sf1 = getTB(0)
            for a in range(n - 2):
                t1, b1 = getTB(a)
                t2, b2 = sf1
                sf1 = [min(t1, t2), max(b1, b2)]
                area1 = getArea(getFromTL(m - 1, a))
                sf2 = getTB(a + 1)
                for b in range(a + 1, n - 1):
                    l1, r1 = getTB(b)
                    l2, r2 = sf2
                    sf2 = [min(l1, l2), max(r1, r2)]
                    area2 = 1
                    if sf2[0] < m:
                        area2 = (b - a) * (sf2[1] - sf2[0] + 1)
                    b1, b2 = getFromRight(b + 1)
                    area3 = getArea(getFromTR(m - 1, b + 1))
                    res = min(res, area1 + area2 + area3)

        for i in range(m - 1):
            for j in range(n - 1):
                a1 = (
                    getArea(getFromTL(i, j))
                    + getArea(getFromTR(i, j + 1))
                    + getArea(getFromBL(i + 1, n - 1))
                )
                a2 = (
                    getArea(getFromBL(i + 1, j))
                    + getArea(getFromBR(i + 1, j + 1))
                    + getArea(getFromTL(i, n - 1))
                )
                a3 = (
                    getArea(getFromTL(i, j))
                    + getArea(getFromBL(i + 1, j))
                    + getArea(getFromTR(m - 1, j + 1))
                )
                a4 = (
                    getArea(getFromTR(i, j + 1))
                    + getArea(getFromBR(i + 1, j + 1))
                    + getArea(getFromTL(m - 1, j))
                )
                res = min(res, a1, a2, a3, a4)

        return res

360