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

import heapq
class Solution:
    def maximumValueSum(self, board: List[List[int]]) -> int:
        
        self.res = float('-inf')

        def DFS(current,o_i,o_j,s):
            if current ==3:
                self.res = max(self.res,s)
            else:

                if current <=0:
                    temp = []
                    #heapq.heapify(temp)
                    for i in range(len(board)):
                        for j in range(len(board[0])):
                            if i not in o_i and j not in o_j:
                                val = board[i][j]
                                temp.append((val,i,j))
                    temp.sort(key=lambda x:-1*x[0])
                    for k in range(4):
                        if k <= len(temp)-1:
                            o_i.append(temp[k][1])
                            o_j.append(temp[k][2])
                            DFS(current+1,o_i,o_j,s+temp[k][0])
                            o_i.pop()
                            o_j.pop()
                    #k = 4
                    #while k <= len(temp)-1 and temp[k][0] == temp[k-1][0]:
                    #    o_i.append(temp[k][1])
                    #    o_j.append(temp[k][2])
                    #    DFS(current+1,o_i,o_j,s+temp[k][0])
                    #    o_i.pop()
                    #    o_j.pop()
                    #    k +=1
                elif current ==1:
                    temp = []
                    #heapq.heapify(temp)
                    for i in range(len(board)):
                        for j in range(len(board[0])):
                            if i not in o_i and j not in o_j:
                                val = board[i][j]
                                temp.append((val,i,j))
                    temp.sort(key=lambda x:-1*x[0])
                    for k in range(2):
                        if k <= len(temp)-1:
                            o_i.append(temp[k][1])
                            o_j.append(temp[k][2])
                            DFS(current+1,o_i,o_j,s+temp[k][0])
                            o_i.pop()
                            o_j.pop()
                    #k = 4
                    #while k <= len(temp)-1 and temp[k][0] == temp[k-1][0]:
                    #    o_i.append(temp[k][1])
                    #    o_j.append(temp[k][2])
                    #    DFS(current+1,o_i,o_j,s+temp[k][0])
                    #    o_i.pop()
                    #    o_j.pop()
                    #    k +=1
                elif current ==2:
                    local_max = float('-inf')
                    for i in range(len(board)):
                        for j in range(len(board[0])):
                            if i not in o_i and j not in o_j:
                                val = board[i][j]
                                if val>=local_max:
                                    temp_i,temp_j = i,j
                                    local_max = val
                    o_i.append(temp_i)
                    o_j.append(temp_j)
                    DFS(current+1,o_i,o_j,s+local_max)
                    o_i.pop()
                    o_j.pop()
                
            
        DFS(0,[],[],0)

        return self.res


80638