<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 MinCostFlow:
    def __init__(self, n):
        self.n = n
        self.edges = []
        self.g = [[] for _ in range(n)]

    def add_edge(self, from_, to, cap, cost):
        forward = [to, cap, cost, len(self.g[to])]
        backward = [from_, 0, -cost, len(self.g[from_])]
        self.g[from_].append(forward)
        self.g[to].append(backward)
        self.edges.append((from_, to, cap, cost))

    def flow(self, s, t, f):
        n = self.n
        dist = [float('inf')] * n
        prevv = [-1] * n
        preve = [-1] * n
        h = [0] * n
        res = 0

        while f > 0:
            dist = [float('inf')] * n
            dist[s] = 0
            que = []
            heapq.heappush(que, (0, s))

            while que:
                d, v = heapq.heappop(que)
                if dist[v] < d:
                    continue
                for i in range(len(self.g[v])):
                    e = self.g[v][i]
                    if e[1] > 0 and dist[e[0]] > dist[v] + e[2] + h[v] - h[e[0]]:
                        dist[e[0]] = dist[v] + e[2] + h[v] - h[e[0]]
                        prevv[e[0]] = v
                        preve[e[0]] = i
                        heapq.heappush(que, (dist[e[0]], e[0]))

            if dist[t] == float('inf'):
                return -1

            for i in range(n):
                h[i] += dist[i]

            d = f
            v = t
            while v != s:
                d = min(d, self.g[prevv[v]][preve[v]][1])
                v = prevv[v]

            f -= d
            res += d * h[t]
            v = t
            while v != s:
                e = self.g[prevv[v]][preve[v]]
                e[1] -= d
                self.g[v][e[3]][1] += d
                v = prevv[v]

        return res

class Solution:
    def maximumValueSum(self, a):
        n = len(a)
        m = len(a[0])
        s = n + m
        t = s + 1
        B = 10**9

        g = MinCostFlow(t + 1)

        for i in range(n):
            g.add_edge(s, i, 1, 0)
        for j in range(m):
            g.add_edge(n + j, t, 1, 0)
        for i in range(n):
            for j in range(m):
                g.add_edge(i, n + j, 1, B - a[i][j])

        cost = g.flow(s, t, 3)
        cost -= 3 * B
        cost = -cost
        return cost



90121