<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>

from sortedcontainers import SortedSet
class Solution:
    def maximumValueSum(self, board):
        m, n = len(board), len(board[0])
        grid = []
        top = []
        bottom = []
        
        set_ = SortedSet(key=lambda x: (-x[0], x[1], x[2]))
        
        for i in range(m):
            t = sorted([(board[i][j], j) for j in range(n)], key=lambda x: -x[0])
            grid.append(t[:3])
            
            for j in range(n):
                set_.add((board[i][j], j, i))
            
            temp = []
            s = set()
            for pair in set_:
                if pair[1] not in s:
                    temp.append((pair[0], pair[1]))
                    s.add(pair[1])
                if len(temp) == 3:
                    break
            top.append(temp)
        
        set_.clear()
        
        for i in range(m - 1, -1, -1):
            for j in range(n):
                set_.add((board[i][j], j, i))
            
            temp = []
            s = set()
            for pair in set_:
                if pair[1] not in s:
                    temp.append((pair[0], pair[1]))
                    s.add(pair[1])
                if len(temp) == 3:
                    break
            bottom.append(temp)
        
        bottom = bottom[::-1]

        ans = - (10 ** 20)
        for i in range(1, m - 1):
            for j in range(3):
                for k in range(3):
                    for l in range(3):
                        curr = grid[i][j]
                        tP = top[i - 1][k]
                        bP = bottom[i + 1][l]
                        
                        c1 = curr[1]
                        c2 = tP[1]
                        c3 = bP[1]
                        
                        v1 = curr[0]
                        v2 = tP[0]
                        v3 = bP[0]
                        
                        if c1 != c2 and c1 != c3 and c2 != c3:
                            ans = max(ans, v1 + v2 + v3)
        
        return ans

72208