<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:
        n, m = len(board), len(board[0])
        res = sum(board[i][i] for i in range(3))
        dp = [[[[(-948794879487, -1)]*2 for _ in range(4)] for _ in range(m+1)] for _ in range(n+1)]
        def helper(arr, v, ind):
            if v >= arr[0][0]:
                return [(v, ind), arr[0]]
            elif v > arr[1][0]:
                return [arr[0], (v, ind)]
            return arr
        def helper2(arr1, arr2):
            if arr1[0][1] != arr2[0][1]:
                return arr1[0][0] + arr2[0][0]
            return max(arr1[0][0] + arr2[1][0], arr1[1][0] + arr2[0][0])
        for i in range(n):
            i2 = n-i-1
            cur1, cur2, cur3, cur4 = board[i][0], board[i][-1], board[i2][0], board[i2][-1]
            for j in range(m):
                cur1 = max(cur1, board[i][j])
                cur3 = max(cur3, board[i2][j])
                dp[i][j][0] = helper(dp[i-1][j][0], cur1, i)
                dp[i2][j][2] = helper(dp[i2+1][j][2], cur3, i2)
            for j in range(m-1, -1, -1):
                cur2 = max(cur2, board[i][j])
                cur4 = max(cur4, board[i2][j])
                dp[i][j][1] = helper(dp[i-1][j][1], cur2, i)
                dp[i2][j][3] = helper(dp[i2+1][j][3], cur4, i2)
        # fix middle points (for x-axis)
        for i in range(n):
            for j in range(m):
                cur = board[i][j]
                if i > 1 and 0 < j < m-1:
                    # type 1, top left and top right
                    res = max(res, cur + helper2(dp[i-1][j-1][0], dp[i-1][j+1][1]))
                if 0 < i < n-1 and 0 < j < m-1:
                    # type 2, top left and bottom right
                    res = max(res, cur + helper2(dp[i-1][j-1][0], dp[i+1][j+1][3]))
                    # type 3, bottom left and top right
                    res = max(res, cur + helper2(dp[i+1][j-1][2], dp[i-1][j+1][1]))
                if i < n-2 and 0 < j < m-1:
                    # type 4, bottom left and bottom right
                    res = max(res, cur + helper2(dp[i+1][j-1][2], dp[i+1][j+1][3]))
        return res
91175