<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:
	dp = []
	@lru_cache
	def sum1(self, i1, j1, i2, j2: int) -> int:
		res = self.dp[i2][j2]
		if i1>0:
			res -= self.dp[i1-1][j2]
		if j1>0:
			res -= self.dp[i2][j1-1]
		if i1>0 and j1>0:
			res += self.dp[i1-1][j1-1]
		return res
	
	@lru_cache
	def calc(self, i1,j1,i2,j2, split: int) -> int:
		if split == 1:
			if self.sum1(i1,j1,i2,j2) == 0 :
				return 0
			while i1<i2:
				if self.sum1(i1,j1,i1,j2) == 0:
					i1+=1
				else:
					break
			while i1<i2:
				if self.sum1(i2,j1,i2,j2) == 0:
					i2-=1
				else:
					break
			while j1<j2:
				if self.sum1(i1,j1,i2,j1) == 0:
					j1+=1
				else:
					break
			while j1<j2:
				if self.sum1(i1,j2,i2,j2) == 0:
					j2-=1
				else:
					break
			return (i2-i1+1)*(j2-j1+1)
		res = 1000
		for i in range(i1,i2):
			if split == 3:
				res = min(res, self.calc(i1,j1,i,j2,1) + self.calc(i+1,j1,i2,j2,2))
				res = min(res, self.calc(i1,j1,i,j2,2) + self.calc(i+1,j1,i2,j2,1))
			else:
				res = min(res, self.calc(i1,j1,i,j2,1) + self.calc(i+1,j1,i2,j2,1))
		for j in range(j1,j2):
			if split == 3:
				res = min(res, self.calc(i1,j1,i2,j,2) + self.calc(i1,j+1,i2,j2,1))
				res = min(res, self.calc(i1,j1,i2,j,1) + self.calc(i1,j+1,i2,j2,2))
			else:
				res = min(res, self.calc(i1,j1,i2,j,1) + self.calc(i1,j+1,i2,j2,1))
		return res

	def minimumSum(self, grid: List[List[int]]) -> int:
		self.dp = [[]]*len(grid)
		for i in range(len(grid)):
			self.dp[i] = [0]*len(grid[0])
		min_i, max_i = len(grid), 0
		min_j, max_j = len(grid[0]), 0
		for i in range(len(grid)):
			for j in range(len(grid[i])):
				if grid[i][j] == 1:
					min_i = min(min_i, i)
					max_i = max(max_i, i)
					min_j = min(min_j, j)
					max_j = max(max_j, j)
		for i in range(len(grid)):
			for j in range(len(grid[0])):
				sum = 0
				if i > 0:
					sum += self.dp[i-1][j]
				if j > 0:
					sum += self.dp[i][j-1]
				if i > 0 and j>0:
					sum -= self.dp[i-1][j-1]
				self.dp[i][j] = sum+grid[i][j]
		return self.calc(min_i, min_j, max_i, max_j, 3)
360