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

        def check(t):
            l = len(t)
            if l == 1:
                return n_occ[(t[0][0], t[0][1])] < 2
            if l == 2:
                for c in seen:
                    if c[:l] == t:
                        return False
                return True
            return True

        

        hq = []
        vals = []
        cols, rows = len(board[0]), len(board)
        for r in range(rows):
            for c in range(cols):
                heappush(hq, (-board[r][c], r, c))
                vals.append((board[r][c], r, c))

        three_max = sorted(vals)[-3:]

        sol = -float("inf")

        seen = []
        n_tested = defaultdict(int)
        n_occ = defaultdict(int)
        for _ in range(8):
            res = 0
            hq1 = hq.copy()
            
            rooks_rem = 3
            cols_used = set()
            rows_used = set()

            cur = []
            pos = True
            while rooks_rem > 0:
                if len(hq1) == 0:
                    pos = False
                    break
                val, r, c = heappop(hq1)
                if r not in rows_used and c not in cols_used and check(cur + [(r, c)]):
                    
                    res -= val
                    rows_used.add(r)
                    cols_used.add(c)
                    rooks_rem -= 1
                    cur.append((r, c))
            seen.append(cur)
            n_occ[cur[0]] += 1

            #print(seen, res)
            if pos:
                sol = max(sol, res)


        print(seen)

        return sol
        
        
        """
        res = 0
        for val, r, c in three_max:
            cols_used = {c}
            rows_used = {r}
            hq1 = hq.copy()
            rooks_rem = 2
            cur_res = val
            while True:
                neg_val, rr, cc = heappop(hq1)
                if rr not in rows_used and cc not in cols_used:

                    cur_res_inner = -neg_val


        return 0


        
        rooks_rem = 3
        res = 0
        cols_used = set()
        rows_used = set()

        while rooks_rem > 0:
            val, r, c = heappop(hq)
            if r not in rows_used and c not in cols_used:
                res -= val
                rows_used.add(r)
                cols_used.add(c)
                rooks_rem -= 1

        return res

        """
66939