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

from collections import deque
import sys

class CostFlow:
    def __init__(self, n, max_edge=0):
        self.INF = sys.maxsize // 2
        self.n = n
        self.e = []
        self.g = [[] for _ in range(n)]
        self.ins = [0] * n
        if max_edge:
            self.e.reserve(2 * max_edge)
        self.dis = []
        self.now = []
        self.s = 0
        self.t = 0
        self.maxflow = 0
        self.mincost = 0

    class Edge:
        def __init__(self, nxt, cap, cost):
            self.nxt = nxt
            self.cap = cap
            self.cost = cost

    def addedge(self, u, v, cap, cost):
        self.g[u].append(len(self.e))
        self.e.append(self.Edge(v, cap, cost))
        self.g[v].append(len(self.e))
        self.e.append(self.Edge(u, 0, -cost))

    def SPFA(self):
        self.dis = [self.INF] * self.n
        inq = [0] * self.n
        que = deque([self.s])
        self.dis[self.s] = 0
        inq[self.s] = 1

        while que:
            x = que.popleft()
            inq[x] = 0
            for p in self.g[x]:
                edge = self.e[p]
                if edge.cap > 0 and self.dis[edge.nxt] > self.dis[x] + edge.cost:
                    self.dis[edge.nxt] = self.dis[x] + edge.cost
                    if not inq[edge.nxt]:
                        inq[edge.nxt] = 1
                        que.append(edge.nxt)

        return self.dis[self.t] != self.INF

    def Dinic(self, x, flow):
        if x == self.t:
            return flow
        self.ins[x] = 1
        rest = flow

        while self.now[x] < len(self.g[x]):
            p = self.g[x][self.now[x]]
            edge = self.e[p]

            if not self.ins[edge.nxt] and edge.cap > 0 and self.dis[edge.nxt] == self.dis[x] + edge.cost:
                incr = self.Dinic(edge.nxt, min(rest, edge.cap))
                if incr == 0:
                    self.dis[edge.nxt] = 0
                edge.cap -= incr
                self.e[p ^ 1].cap += incr
                rest -= incr
                self.mincost += incr * edge.cost
                if rest == 0:
                    break

            self.now[x] += 1

        self.ins[x] = 0
        return flow - rest

    def mcmf(self, s, t):
        self.s = s
        self.t = t
        self.maxflow = 0
        self.mincost = 0
        flow = 0

        while self.SPFA():
            self.now = [0] * self.n
            while True:
                flow = self.Dinic(s, self.INF)
                if flow == 0:
                    break
                self.maxflow += flow

        return self.maxflow, self.mincost


class Solution:
    def maximumValueSum(self, a):
        n = len(a)
        m = len(a[0])
        G = CostFlow(n + m + 3)
        S = n + m
        T = S + 1
        TT = T + 1

        for i in range(n):
            G.addedge(S, i, 1, 0)

        for i in range(n):
            for j in range(m):
                G.addedge(i, j + n, 1, -a[i][j])

        for j in range(m):
            G.addedge(j + n, T, 1, 0)

        G.addedge(T, TT, 3, 0)
        return -G.mcmf(S, TT)[1]

89068