<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:
        result = float('-inf')

        n = len(board)
        m = len(board[0])
        rows = [False] * n
        cols = [False] * m

        cell_by_row = defaultdict(list)

        for row, line in enumerate(board):
            for col, val in enumerate(line):
                cell_by_row[row].append((val, row, col))

        cells = []
        for values in cell_by_row.values():
            values.sort(reverse=True)
            for pos in range(3):
                cells.append(values[pos])

        cells.sort(reverse=True)

        def check(row, col):
            return not rows[row] and not cols[col]

        def place_rook(row, col):
            cols[col] = rows[row] = True

        def remove_rook(row, col):
            cols[col] = rows[row] = False


        cnt = n * m
        result = float('-inf')


        # one_val, one_row, one_col = cells[0]
        # place_rook(one_row, one_col)
        # two_val, two_row, two_col = cells[1]
        # if check(two_row, two_col):
        #     place_rook(two_row, two_col)
        #     three_val, three_row, three_col = cells[2]
        #     if check(three_row, three_col):
        #         return one_val + two_val + three_val
        #     remove_rook(two_row, two_col)
        # remove_rook(one_row, one_col)


        for one in range(n):
            one_val, one_row, one_col = cells[one]
            place_rook(one_row, one_col)
            for two in range(one + 1, n + 2):
                two_val, two_row, two_col = cells[two]
                if check(two_row, two_col):
                    place_rook(two_row, two_col)
                    #for three_val, three_row, three_col in cells:
                    for three in range(one + 1, len(cells)):
                        three_val, three_row, three_col = cells[three]
                        if check(three_row, three_col):
                            result = max(result, one_val + two_val + three_val)
                            break                    
                    remove_rook(two_row, two_col)
            remove_rook(one_row, one_col)

        return result
44810