<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, g: List[List[int]]) -> int:
        n = len(g)
        m = len(g[0])
        ans = -1e18

        pref = [[-1e9] * (m + 1) for _ in range(n + 1)]
        suf = [[-1e9] * (m + 1) for _ in range(n + 1)]
        pref2 = [[-1e9] * (m + 1) for _ in range(n + 1)]
        suf2 = [[-1e9] * (m + 1) for _ in range(n + 1)]
        
        for i in range(n):
            for j in range(m):
                pref[i + 1][j + 1] = max(max(pref[i][j + 1], pref[i + 1][j]), g[i][j])
        
        for i in range(n):
            for j in range(m - 1, -1, -1):
                suf[i + 1][j] = max(max(suf[i + 1][j + 1], suf[i][j]), g[i][j])
        
        for i in range(n - 1, -1, -1):
            for j in range(m):
                pref2[i][j + 1] = max(max(pref2[i + 1][j + 1], pref2[i][j]), g[i][j])
        
        for i in range(n - 1, -1, -1):
            for j in range(m - 1, -1, -1):
                suf2[i][j] = max(max(suf2[i + 1][j], suf2[i][j + 1]), g[i][j])
        
        row_pref = [[-1e9] * (m + 1) for _ in range(n)]
        row_suf = [[-1e9] * (m + 1) for _ in range(n)]
        
        for i in range(n):
            for j in range(m):
                row_pref[i][j + 1] = max(row_pref[i][j], g[i][j])
            for j in range(m - 1, -1, -1):
                row_suf[i][j] = max(row_suf[i][j + 1], g[i][j])
        
        for j in range(m):
            x = -2e9
            for i in range(1, n):
                x = max(x, row_pref[i - 1][j] + suf[i - 1][j + 1])
                ans = max(ans, g[i][j] + x)
        
            x = -2e9
            for i in range(1, n):
                x = max(x, row_suf[i - 1][j + 1] + pref[i - 1][j])
                ans = max(ans, g[i][j] + x)
        
        for j in range(m):
            x = -2e9
            for i in range(n - 2, -1, -1):
                x = max(x, row_pref[i + 1][j] + suf2[i + 2][j + 1])
                ans = max(ans, g[i][j] + x)
            
            x = -2e9
            for i in range(n - 2, -1, -1):
                x = max(x, row_suf[i + 1][j + 1] + pref2[i + 2][j])
                ans = max(ans, g[i][j] + x)
        
        for i in range(n):
            for j in range(m):
                ans = max(ans, g[i][j] + pref[i][j] + suf2[i + 1][j + 1])
                ans = max(ans, g[i][j] + pref2[i + 1][j] + suf[i][j + 1])
        
        return int(ans)
37434