<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:
        R = len(board)
        C = len(board[0])
        INF = 10 ** 20
        
        items = []
        for r, row in enumerate(board):
            items.extend(list(sorted(((x, index) for index, x in enumerate(row)))[-3:]))
        items.sort()
        lower_bound = items[0][0]
        if len(items) > 20:
            lower_bound = items[-20][0]
                
        cols = collections.defaultdict(set)
        for j in range(C):
            current_cols = []
            for i in range(R):
                current_cols.append((board[i][j], j, i))
            for x, c, r in list(sorted(current_cols))[-3:]:
                cols[c].add(r)
        
        new_board = []
        for r, row in enumerate(board):
            current = list(sorted(((x, index) for index, x in enumerate(row) if r in cols[index] and x >= lower_bound))[-3:])
            if len(current) > 0:
                new_board.append(current)

        R = len(new_board)
        
        @cache
        def go(row, count, c1, c2):
            if count == 3:
                return 0
            
            if row == R:
                return -INF
            
            best = -INF
            for x, index in new_board[row]:
                if index != c1 and index != c2:
                    if c1 == -1:
                        best = max(best, go(row + 1, count + 1, index, -1) + x)
                    elif c2 == -1:
                        best = max(best, go(row + 1, count + 1, c1, index) + x)
                    else:
                        best = max(best, go(row + 1, count + 1, c1, c2) + x)
            best = max(best, go(row + 1, count, c1, c2))
            return best
        
        return go(0, 0, -1, -1)
46918