<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:
        m = len(board)
        n = len(board[0])
        arr = [[[board[_][i],i] for i in range(n)] for _ in range(m)]

        def choosefrom3(r1,r2,r3):
            arr1,arr2,arr3 = sorted(arr[r1],reverse = True), sorted(arr[r2],reverse = True), sorted(arr[r3],reverse = True)
            s = set()
            for i in range(3):
                s.add(arr1[i][1])
                s.add(arr2[i][1])
                s.add(arr3[i][1])
            s = list(s)
            ll = len(s)
            maxval = -float(inf)
            maxind = [-1,-1,-1]
            #print(s,ll)
            for i in range(ll):
                for j in range(ll):
                    if j == i:
                        continue
                    for k in range(ll):
                        if k == i or k == j:
                            continue
                        if board[r1][s[i]] + board[r2][s[j]] + board[r3][s[k]] > maxval:
                            maxval = board[r1][s[i]] + board[r2][s[j]] + board[r3][s[k]]
                            maxind = [s[i],s[j],s[k]]
            return maxind, maxval
        
        cind, res = choosefrom3(0,1,2)
        rind = [0,1,2]
        thres = min(board[rind[0]][cind[0]], board[rind[1]][cind[1]], board[rind[2]][cind[2]])
        for rr in range(3,m):
            c1, r1 = choosefrom3(rind[0],rind[1],rr)
            c2, r2 = choosefrom3(rind[0],rind[2],rr)
            c3, r3 = choosefrom3(rind[1],rind[2],rr)
            rnew = max(r1,r2,r3)
            if rnew <= res:
                continue
            res = rnew
            if r1 == rnew:
                rind = [rind[0],rind[1],rr]
                cind = c1
            elif r2 == rnew:
                rind = [rind[0],rind[2],rr]
                cind = c2
            elif r3 == rnew:
                rind = [rind[1],rind[2],rr]
                cind = c3

        return res      
49025