<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
from typing import List, Tuple

class Graph:
    def __init__(self, vertex_cnt, edge_cnt):
        self.vertex_cnt = vertex_cnt
        self.edges = []
        self.adj = [[] for _ in range(vertex_cnt)]

    def add_edge(self, from_vertex, to_vertex, cap, cost):
        self.edges.append([from_vertex, to_vertex, cap, cost])
        self.adj[from_vertex].append(len(self.edges) - 1)
        self.edges.append([to_vertex, from_vertex, 0, -cost])
        self.adj[to_vertex].append(len(self.edges) - 1)

    def min_cost_flow(self, source, sink, flow_limit=float('inf')):
        potential = [0] * self.vertex_cnt
        dist = [float('inf')] * self.vertex_cnt
        prev_vertex = [0] * self.vertex_cnt
        prev_edge = [0] * self.vertex_cnt

        flow = 0
        cost = 0
        while flow < flow_limit:
            dist = [float('inf')] * self.vertex_cnt
            dist[source] = 0
            heap = [(0, source)]  # (distance, node)
            while heap:
                d, v = heapq.heappop(heap)
                if d > dist[v]:
                    continue
                for i in self.adj[v]:
                    e = self.edges[i]
                    if e[2] > 0 and dist[e[1]] > dist[v] + e[3] + potential[v] - potential[e[1]]:
                        dist[e[1]] = dist[v] + e[3] + potential[v] - potential[e[1]]
                        prev_vertex[e[1]] = v
                        prev_edge[e[1]] = i
                        heapq.heappush(heap, (dist[e[1]], e[1]))

            if dist[sink] == float('inf'):
                break

            for i in range(self.vertex_cnt):
                potential[i] += dist[i] if dist[i] < float('inf') else 0

            add_flow = flow_limit - flow
            v = sink
            while v != source:
                add_flow = min(add_flow, self.edges[prev_edge[v]][2])
                v = prev_vertex[v]

            flow += add_flow
            cost += add_flow * potential[sink]
            v = sink
            while v != source:
                self.edges[prev_edge[v]][2] -= add_flow
                self.edges[prev_edge[v] ^ 1][2] += add_flow
                v = prev_vertex[v]

        return flow, cost

class Solution:
    def maximumValueSum(self, a: List[List[int]]) -> int:
        n = len(a)
        m = len(a[0])
        G = Graph(n + m + 3, n * m + n + m + 3)
        S = n + m
        T = S + 1
        TT = T + 1
        for i in range(n):
            G.add_edge(S, i, 1, 0)
        for i in range(n):
            for j in range(m):
                G.add_edge(i, j + n, 1, -a[i][j])
        for j in range(m):
            G.add_edge(j + n, T, 1, 0)
        G.add_edge(T, TT, 3, 0)
        return -G.min_cost_flow(S, TT)[1]
86960