<p>You are given a <code>m x n</code> 2D array <code>board</code> representing a chessboard, where <code>board[i][j]</code> represents the <strong>value</strong> of the cell <code>(i, j)</code>.</p>

<p>Rooks in the <strong>same</strong> row or column <strong>attack</strong> each other. You need to place <em>three</em> rooks on the chessboard such that the rooks <strong>do not</strong> <strong>attack</strong> each other.</p>

<p>Return the <strong>maximum</strong> sum of the cell <strong>values</strong> on which the rooks are placed.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = </span>[[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]</p>

<p><strong>Output:</strong> 4</p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/08/rooks2.png" style="width: 294px; height: 450px;" /></p>

<p>We can place the rooks in the cells <code>(0, 2)</code>, <code>(1, 3)</code>, and <code>(2, 1)</code> for a sum of <code>1 + 1 + 2 = 4</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[1,2,3],[4,5,6],[7,8,9]]</span></p>

<p><strong>Output:</strong> <span class="example-io">15</span></p>

<p><strong>Explanation:</strong></p>

<p>We can place the rooks in the cells <code>(0, 0)</code>, <code>(1, 1)</code>, and <code>(2, 2)</code> for a sum of <code>1 + 5 + 9 = 15</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[1,1,1],[1,1,1],[1,1,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>We can place the rooks in the cells <code>(0, 2)</code>, <code>(1, 1)</code>, and <code>(2, 0)</code> for a sum of <code>1 + 1 + 1 = 3</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= m == board.length &lt;= 100</code></li>
	<li><code>3 &lt;= n == board[i].length &lt;= 100</code></li>
	<li><code>-10<sup>9</sup> &lt;= board[i][j] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maximumValueSum(self, board: List[List[int]]) -> int:
        m, n = len(board), len(board[0])
        cands = sorted(product(range(m), range(n)), key = lambda p: -board[p[0]][p[1]])[:2*(m+n)]
        getNum = lambda p1,p2,p3: -board[cands[p1][0]][cands[p1][1]]-board[cands[p2][0]][cands[p2][1]]-board[cands[p3][0]][cands[p3][1]]
        l, v = len(cands), set([(0,1,2)])
        heap = [(getNum(0,1,2), 0, 1, 2)]

        while heap:
            res, r,s,t = heappop(heap)

            (i1, j1), (i2, j2), (i3, j3) = cands[r], cands[s], cands[t]

            if len({i1,i2,i3}) == len({j1,j2,j3}) == 3:
                return -res
            
            if r+1 < s and (r+1, s, t) not in v:
                v.add((r+1, s, t))
                heappush(heap, (getNum(r+1,s,t), r+1,s,t))
            if s+1 < t and (r, s+1, t) not in v:
                v.add((r, s+1, t))
                heappush(heap, (getNum(r,s+1,t), r,s+1,t))
            if t+1 < l and (r, s, t+1) not in v:
                v.add((r, s, t+1))
                heappush(heap, (getNum(r,s,t+1), r,s,t+1))
        return -1

171998