<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;= 500</code></li>
	<li><code>3 &lt;= n == board[i].length &lt;= 500</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])
        def update(row: List[int]) -> None:
            for j, x in enumerate(row):
                if x > mx[0][0]:
                    if j != mx[0][1]:     # 如果和之前的mx[0]列相同，不需要更新 mx[1]
                        if j != mx[1][1]: # 如果和之前mx[1]列相同，第三大的数保留，原第二大drop
                            mx[2] = mx[1]
                        mx[1] = mx[0]
                    mx[0] = (x, j)
                elif x > mx[1][0] and j != mx[0][1]:
                    if j != mx[1][1]:
                        mx[2] = mx[1]
                    mx[1] = (x, j)
                elif x > mx[2][0] and j != mx[0][1] and j != mx[1][1]:
                    mx[2] = (x, j)

        # suffix
        suf = [None] * m
        mx = [(-inf, -1)] * 3
        for i in range(m-1, 1, -1):
            update(board[i])
            suf[i] = mx[:]      # just a shallow copy
        # print(suf)
        pre = [None] * m
        mx = [(-inf, -1)] * 3
        ans = -inf
        for i in range(0, m-2):
            update(board[i])
            pre[i] = mx[:]
            # print("pre", pre[i])
            # print("pre", mx)
            # print("cur", board[i+1])
            # print("nxt", suf[i+2])
            for j2, y in enumerate(board[i+1]):
                # for x, j1 in pre[i]:     # 会改变 suf array, 因为 suf[i] = pre[:] 是浅拷贝
                for x, j1 in mx:
                    for z, j3 in suf[i+2]:
                        if j1 != j2 and j1 != j3 and j2 != j3:
                            s = x + y + z
                            if s > ans:
                                ans = s
                            break
        # 此时 pre 和 suf 是相同的，浅拷贝引用
        return ans
39541