<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 = len(board)
        n = len(board[0])
        upmax = [[None for j in range(n)] for i in range(m)]
        for j in range(n):
            upmax[1][j] = board[0][j]
        for i in range(2, m):
            for j in range(n):
                upmax[i][j] = max(upmax[i - 1][j], board[i - 1][j])
        downmax = [[None for j in range(n)] for i in range(m)]
        for j in range(n):
            downmax[m - 2][j] = board[m - 1][j]
        for i in range(m - 3, -1, -1):
            for j in range(n):
                downmax[i][j] = max(downmax[i + 1][j], board[i + 1][j])
        preleftupmax = [[None for j in range(n)] for i in range(m)]
        preleftdownmax = [[None for j in range(n)] for i in range(m)]
        for i in range(1, m - 1):
            preleftupmax[i][1] = upmax[i][0]
            preleftdownmax[i][1] = downmax[i][0]
            for j in range(2, n):
                preleftupmax[i][j] = max(preleftupmax[i][j - 1], upmax[i][j - 1])
                preleftdownmax[i][j] = max(preleftdownmax[i][j - 1], downmax[i][j - 1])
        prerightupmax = [[None for j in range(n)] for i in range(m)]
        prerightdownmax = [[None for j in range(n)] for i in range(m)]
        for i in range(1, m - 1):
            prerightupmax[i][n - 2] = upmax[i][n - 1]
            prerightdownmax[i][n - 2] = downmax[i][n - 1]
            for j in range(n - 3, -1, -1):
                prerightupmax[i][j] = max(prerightupmax[i][j + 1], upmax[i][j + 1])
                prerightdownmax[i][j] = max(prerightdownmax[i][j + 1], downmax[i][j + 1])
        rightsectionmax = [[None for j in range(n)] for i in range(m)]
        leftsectionmax = [[None for j in range(n)] for i in range(m)]
        for i in range(1, m - 1):
            leftsectionmax[i][2] = max(upmax[i][1] + preleftdownmax[i][1], downmax[i][1] + preleftupmax[i][1])
            for j in range(3, n):
                leftsectionmax[i][j] = max(upmax[i][j - 1] + preleftdownmax[i][j - 1], downmax[i][j - 1] + preleftupmax[i][j - 1])
                leftsectionmax[i][j] = max(leftsectionmax[i][j], leftsectionmax[i][j - 1])
            rightsectionmax[i][n - 3] = max(upmax[i][n - 2] + prerightdownmax[i][n - 2], downmax[i][n - 2] + prerightupmax[i][n - 2])
            for j in range(n - 4, -1, -1):
                rightsectionmax[i][j] = max(upmax[i][j + 1] + prerightdownmax[i][j + 1], downmax[i][j + 1] + prerightupmax[i][j + 1])
                rightsectionmax[i][j] = max(rightsectionmax[i][j], rightsectionmax[i][j + 1])
        res = board[0][0] + board[1][1] + board[2][2]
        for i in range(m):
            for j in range(n):
                if preleftupmax[i][j] != None and prerightdownmax[i][j] != None:
                    res = max(res, board[i][j] + preleftupmax[i][j] + prerightdownmax[i][j])
                if preleftdownmax[i][j] != None and prerightupmax[i][j] != None:    
                    res = max(res, board[i][j] + preleftdownmax[i][j] + prerightupmax[i][j])
                if rightsectionmax[i][j] != None:
                    res = max(res, board[i][j] + rightsectionmax[i][j])
                if leftsectionmax[i][j] != None:
                    res = max(res, board[i][j] + leftsectionmax[i][j])
        return res
                
58509