<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 get(dp, i,j):
            if 0<=i<M and 0<=j<N:
                return dp[i][j]
            return -inf

        L = [-inf]*N 
        R = [-inf]*N
        dp1L = [[-inf]*N for _ in range(M)]
        dp1R = [[-inf]*N for _ in range(M)]
        dp2L = [[-inf]*N for _ in range(M)]
        dp2R = [[-inf]*N for _ in range(M)]
        dp2LR = [[-inf]*N for _ in range(M)]
        maxCol = [[-inf]*N for _ in range(M)]

        for i in range(M):
            for j in range(N):
                dp1L[i][j] = max(
                    get(dp1L,i-1,j), 
                    get(dp1L,i,j-1), 
                    board[i][j]
                )
            for j in range(N-1,-1,-1):
                dp1R[i][j] = max(
                    get(dp1R,i-1,j), 
                    get(dp1R,i,j+1), 
                    board[i][j]
                )

        res = -inf
        for i in range(M):
            for j in range(N):
                maxCol[i][j] = max(get(maxCol, i-1,j), board[i][j])
            L = [board[i][0]] + [-inf]*(N-1)
            for j in range(1,N):
                L[j] = max(L[j-1], board[i][j])
            R = [-inf]*(N-1) + [board[i][-1]]
            for j in range(N-2,-1,-1):
                R[j] = max(R[j+1], board[i][j])

            for j in range(N):
                dp2L[i][j] = max(
                    get(dp2L,i,j-1),
                    get(dp2L,i-1,j),
                    get(dp1L,i-1,j-1) + board[i][j],
                    get(maxCol,i-1, j) + (L[j-1] if j>0 else -inf)
                )
            for j in range(N-1,-1,-1):
                dp2R[i][j] = max(
                    get(dp2R,i,j+1),
                    get(dp2R,i-1,j),
                    get(dp1R,i-1,j+1) + board[i][j],
                    get(maxCol,i-1,j) + (R[j+1] if j+1<N else -inf)
                )
            
            
            for j in range(1,N-1):
                dp2LR[i][j] = max(
                    get(dp2LR, i-1,j),
                    L[j-1] + get(dp1R, i-1,j+1),
                    get(dp1L, i-1,j-1) + R[j+1]
                )

            for j in range(N):
                cur = max(
                    get(dp2L, i-1,j-1),
                    get(dp2R, i-1,j+1),
                    dp2LR[i-1][j]
                )
                res = max(res, cur+board[i][j])
            
        
        return res
43756