<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,n,oo = len(board),len(board[0]),-10**9-1
        for i,row in enumerate(board): # max 3
            r = [(oo,-1)]*3
            for c,v in enumerate(row):
                if v<=r[-1][0]: continue
                elif v>r[0][0]: r = [(v,c)]+r[:2]
                elif v>r[1][0]: r[1:] = [(v,c),r[1]]
                else: r[2] = (v,c)
            board[i] = r
        # prefix/suffix max-3
        def merge(p,q):
            r,d = [],set()
            for e in sorted(p+q,reverse=True):
                if e[1] not in d:
                    r.append(e)
                    d.add(e[1])
                    if len(r)==3: return r
        #
        pref = [board[0][:]]
        for row in board[1:]:
            pref.append(merge(pref[-1],row))
        suff = [board[m-1][:]]
        for row in board[-2::-1]:
            suff.append(merge(suff[-1],row))
        # sweep middle
        opt = -10**10
        for mid in range(1,m-1):
            r1 = board[mid]
            r0 = pref[mid-1]
            r2 = suff[m-2-mid]
            for v0,c0 in r0:
                for v1,c1 in r1:
                    if c0==c1: continue
                    t = v0+v1
                    for v2,c2 in r2:
                        if c2!=c0 and c2!=c1:
                            opt = max(opt,t+v2)
            '''
            for i,j,k in product(range(3),repeat=3):
                if r0[i][1]==r1[j][1] or r0[i][1]==r2[k][1]\
                    or r1[j][1]==r2[k][1]: continue
                opt = max(opt,r0[i][0]+r1[j][0]+r2[k][0])
            '''
        #
        return opt



35326