Problem:
 Score : 600 points    Problem Statement There is a board with N rows and M columns.
The information of this board is represented by N strings S_1,S_2,\ldots,S_N .
Specifically, the state of the square at the i -th row from the top and the j -th column from the left is represented as follows:   S_{i,j}= . : the square is empty.  S_{i,j}= # : an obstacle is placed on the square.  S_{i,j}= o : a piece is placed on the square.   Yosupo repeats the following operation:   Choose a piece and move it to its right adjecent square or its down adjacent square.
Moving a piece to squares with another piece or an obstacle is prohibited.
Moving a piece out of the board is also prohibited.   Yosupo wants to perform the operation as many times as possible.
Find the maximum possible number of operations.      Constraints  1 \leq N \leq 50  1 \leq M \leq 50  S_i is a string of length M consisting of . , # and o .  1 \leq ( the number of pieces )\leq 100 .
In other words, the number of pairs (i, j) that satisfy S_{i,j}= o is between 1 and 100 , both inclusive.         Input Input is given from Standard Input in the following format:  N  M  S_1  S_2  \vdots  S_N       Output Print the maximum possible number of operations in a line.        Sample Input 1 3 3
o..
...
o.#      Sample Output 1 4  Yosupo can perform operations 4 times as follows:  o..      .o.      ..o      ...      ...
...  ->  ...  ->  ...  ->  ..o  ->  ..o
o.#      o.#      o.#      o.#      .o#       Sample Input 2 9 10
.#....o#..
.#..#..##o
.....#o.##
.###.#o..o
#.#...##.#
..#..#.###
#o.....#..
....###..o
o.......o#      Sample Output 2 24

Solution (Python):
import networkx as nx

n,m=map(int,input().split())
board=[input() for _ in range(n)]
k=0
for i in range(n):
  for j in range(m):
    if board[i][j]=='o':
      k+=1
INF=10**9
g=nx.DiGraph()
g.add_node(0,demand=k)
for i in range(n):
  for j in range(m):
    if board[i][j]=='
      continue
    if board[i][j]=='o':
      g.add_node((i,j),demand=-1)
      g.add_edge((i,j),0,capacity=1,weight=0)
    elif board[i][j]=='.':
      g.add_node((i,j),demand=0)
      g.add_edge((i,j),0,capacity=1,weight=0)
    if i+1<n and board[i+1][j]!='
      g.add_edge((i,j),(i+1,j),weight=-1,capacity=INF)
    if j+1<m and board[i][j+1]!='
      g.add_edge((i,j),(i,j+1),weight=-1,capacity=INF)
flow=nx.min_cost_flow(g)
result=sum(v*g[k1][k2]['weight'] for k1,dc in flow.items() for k2,v in dc.items())
print(-result)

Solution (Python):

from heapq import heappush, heappop
class MinCostFlow:
    INF = 10**18

    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap, cost):
        forward = [to, cap, cost, None]
        backward = forward[3] = [fr, 0, -cost, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def flow(self, s, t, f):
        N = self.N; G = self.G
        INF = MinCostFlow.INF

        res = 0
        H = [0]*N
        prv_v = [0]*N
        prv_e = [None]*N

        d0 = [INF]*N
        dist = [INF]*N

        while f:
            dist[:] = d0
            dist[s] = 0
            que = [(0, s)]

            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w, cap, cost, _ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prv_v[w] = v; prv_e[w] = e
                        heappush(que, (r, w))
            if dist[t] == INF:
                return None

            for i in range(N):
                H[i] += dist[i]

            d = f; v = t
            while v != s:
                d = min(d, prv_e[v][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prv_e[v]
                e[1] -= d
                e[3][1] += d
                v = prv_v[v]
        return res



from sys import stdin
def input():
    return stdin.readline().strip()

def main():
    n, m = map(int, input().split())
    s = [input() for _ in range(n)]

    mcf = MinCostFlow(n*m + 2)

    source = n * m
    sink = n * m + 1

    ans = 0
    cnt = 0
    for i in range(n):
        for j in range(m):
            if s[i][j] == '
                continue

            ind = m * i + j

            
            if s[i][j] == 'o':
                ans -= i + j
                cnt += 1
                mcf.add_edge(source, ind, 1, 0)

            mcf.add_edge(ind, sink, 1, -i-j)

            if i < n - 1 and s[i+1][j] != '
                mcf.add_edge(ind, m*(i+1) + j, 100, 0)
            if j < m - 1 and s[i][j+1] != '
                mcf.add_edge(ind, m*i + j+1, 100, 0)

    print(ans - mcf.flow(source, sink, cnt))

main()

Solution (Python):
import os
import sys
from heapq import heappop, heappush

import numpy as np


def solve(n, m, field):
    MINCOSTFLOW_LINKS = []
    INF = 10 ** 18

    def mincostflow_init(n):
        """ n: 頂点数 """
        lst = [[0]]
        lst.clear()
        MINCOSTFLOW_LINKS.append([lst.copy() for _ in range(n)])
        return len(MINCOSTFLOW_LINKS) - 1

    def mincostflow_add_link(ins, frm, to, capacity, cost):
        """ インスタンスID, 辺始点頂点番号, 辺終点頂点番号, 容量, コスト """
        links = MINCOSTFLOW_LINKS[ins]
        links[frm].append([to, capacity, cost, len(links[to])])
        links[to].append([frm, 0, -cost, len(links[frm]) - 1])

    def mincostflow_flow(ins, s, t, quantity):
        """ インスタンスID, フロー始点頂点番号, フロー終点頂点番号, 要求流量 """
        links = MINCOSTFLOW_LINKS[ins]
        n = len(links)
        res = 0
        potentials = np.zeros(n, dtype=np.int64)
        dist = np.full(n, INF, dtype=np.int64)
        prev_v = np.full(n, -1, dtype=np.int64)
        prev_e = np.full(n, -1, dtype=np.int64)

        while quantity:
            dist.fill(INF)
            dist[s] = 0
            que = [(0, s)]

            while que:
                total_cost, v = heappop(que)
                if dist[v] < total_cost:
                    continue
                for i, (u, cap, cost, _) in enumerate(links[v]):
                    new_cost = dist[v] + potentials[v] - potentials[u] + cost
                    if cap > 0 and new_cost < dist[u]:
                        dist[u] = new_cost
                        prev_v[u] = v
                        prev_e[u] = i
                        heappush(que, (new_cost, u))

            
            if dist[t] == INF:
                return -1

            potentials += dist

            cur_flow = quantity
            v = t
            while v != s:
                cur_flow = min(cur_flow, links[prev_v[v]][prev_e[v]][1])
                v = prev_v[v]
            quantity -= cur_flow
            res += cur_flow * potentials[t]

            v = t
            while v != s:
                link = links[prev_v[v]][prev_e[v]]
                link[1] -= cur_flow
                links[v][link[3]][1] += cur_flow
                v = prev_v[v]

        return res

    nm = (n + 2) * (m + 2)
    m2 = m + 2

    starts = np.where(field == 1)[0]
    s_size = starts.size

    ins = mincostflow_init(nm + 2)
    s = nm
    t = s + 1

    for i in starts:
        x, y = divmod(i, m2)
        mincostflow_add_link(ins, s, i, 1, x + y - 2)

    for i in range(nm):
        if field[i] == 2:
            continue
        x, y = divmod(i, m2)
        mincostflow_add_link(ins, i, t, 1, n + m - x - y)
        if field[i + 1] != 2:
            mincostflow_add_link(ins, i, i + 1, INF, 0)
        if field[i + m2] != 2:
            mincostflow_add_link(ins, i, i + m2, INF, 0)

    f = mincostflow_flow(ins, s, t, s_size)

    ans = (n + m - 2) * s_size - f
    return ans


SIGNATURE = '(i8,i8,i1[:],)'
if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', SIGNATURE)(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    
    from my_module import solve
else:
    from numba import njit

    solve = njit(SIGNATURE, cache=True)(solve)
    print('compiled', file=sys.stderr)

n, m = map(int, sys.stdin.readline().split())
field = '
field = np.fromiter(map('.o
ans = solve(n, m, field)
print(ans)

Solution (Python):
import os
import sys
from heapq import heappop, heappush

import numpy as np


def solve(n, m, field):
    MINCOSTFLOW_LINKS = []
    INF = 10 ** 18

    def mincostflow_init(n):
        """ n: 頂点数 """
        lst = [[0]]
        lst.clear()
        MINCOSTFLOW_LINKS.append([lst.copy() for _ in range(n)])
        return len(MINCOSTFLOW_LINKS) - 1

    def mincostflow_add_link(ins, frm, to, capacity, cost):
        """ インスタンスID, 辺始点頂点番号, 辺終点頂点番号, 容量, コスト """
        links = MINCOSTFLOW_LINKS[ins]
        links[frm].append([to, capacity, cost, len(links[to])])
        links[to].append([frm, 0, -cost, len(links[frm]) - 1])

    def mincostflow_flow(ins, s, t, quantity):
        """ インスタンスID, フロー始点頂点番号, フロー終点頂点番号, 要求流量 """
        links = MINCOSTFLOW_LINKS[ins]
        n = len(links)
        res = 0
        potentials = np.zeros(n, dtype=np.int64)
        dist = np.full(n, INF, dtype=np.int64)
        prev_v = np.full(n, -1, dtype=np.int64)
        prev_e = np.full(n, -1, dtype=np.int64)

        while quantity:
            dist.fill(INF)
            dist[s] = 0
            que = [(0, s)]

            while que:
                total_cost, v = heappop(que)
                if dist[v] < total_cost:
                    continue
                for i, (u, cap, cost, _) in enumerate(links[v]):
                    new_cost = dist[v] + potentials[v] - potentials[u] + cost
                    if cap > 0 and new_cost < dist[u]:
                        dist[u] = new_cost
                        prev_v[u] = v
                        prev_e[u] = i
                        heappush(que, (new_cost, u))

            
            if dist[t] == INF:
                return -1

            potentials += dist

            cur_flow = quantity
            v = t
            while v != s:
                cur_flow = min(cur_flow, links[prev_v[v]][prev_e[v]][1])
                v = prev_v[v]
            quantity -= cur_flow
            res += cur_flow * potentials[t]

            v = t
            while v != s:
                link = links[prev_v[v]][prev_e[v]]
                link[1] -= cur_flow
                links[v][link[3]][1] += cur_flow
                v = prev_v[v]

        return res

    nm = (n + 2) * (m + 2)
    m2 = m + 2

    starts = np.where(field == 1)[0]
    s_size = starts.size

    ins = mincostflow_init(nm + 2)
    s = nm
    t = s + 1

    POTENCIAL = 10000

    for i in starts:
        x, y = divmod(i, m2)
        mincostflow_add_link(ins, s, i, 1, (x + y - 2) * POTENCIAL)

    for i in range(nm):
        if field[i] == 2:
            continue
        x, y = divmod(i, m2)
        mincostflow_add_link(ins, i, t, 1, (n + m - x - y) * POTENCIAL)
        if field[i + 1] != 2:
            mincostflow_add_link(ins, i, i + 1, INF, POTENCIAL - 1)
        if field[i + m2] != 2:
            mincostflow_add_link(ins, i, i + m2, INF, POTENCIAL - 1)

    f = mincostflow_flow(ins, s, t, s_size)

    ans = (n + m - 2) * POTENCIAL * s_size - f
    return ans


SIGNATURE = '(i8,i8,i1[:],)'
if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', SIGNATURE)(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    
    from my_module import solve
else:
    from numba import njit

    
    print('compiled', file=sys.stderr)

n, m = map(int, sys.stdin.readline().split())
field = '
field = np.fromiter(map('.o
ans = solve(n, m, field)
print(ans)

Solution (Python):
from heapq import heappush, heappop
class MinCostFlow:
    INF = 10**18

    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def addEdge(self, fr, to, cap, cost):
        forward = [to, cap, cost, None]
        backward = forward[3] = [fr, 0, -cost, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def flow(self, s, t, f):
        N = self.N; G = self.G
        INF = MinCostFlow.INF

        res = 0
        H = [0]*N
        prv_v = [0]*N
        prv_e = [None]*N

        d0 = [INF]*N
        dist = [INF]*N

        while f:
            dist[:] = d0
            dist[s] = 0
            que = [(0, s)]

            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w, cap, cost, _ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prv_v[w] = v; prv_e[w] = e
                        heappush(que, (r, w))
            if dist[t] == INF:
                return None

            for i in range(N):
                H[i] += dist[i]

            d = f; v = t
            while v != s:
                d = min(d, prv_e[v][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prv_e[v]
                e[1] -= d
                e[3][1] += d
                v = prv_v[v]
        return res

G=MinCostFlow(2502)
n,m=map(int,input().split())
S=[]
for _ in range(n):
    S.append(input())
    
os=0

for i in range(n):
    for q in range(m):
        cnt=i*m+q+1
        now=S[i][q]
        if now=="
            continue
        if now=="o":
            G.addEdge(0,cnt,1,0)
            os+=1
        if q<m-1 and S[i][q+1]!="
            G.addEdge(cnt,cnt+1,1000,-1)
        if i<n-1 and S[i+1][q]!="
            G.addEdge(cnt,cnt+m,1000,-1)
        G.addEdge(cnt,2501,1,0)

ans=G.flow(0,2501,os)
print(-ans)

Solution (Python):
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c for j in range(b)] for i in range(a)]
def list3d(a, b, c, d): return [[[d for k in range(c)] for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10**9)
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10

def build_grid(H, W, intv, _type, space=False, padding=True):
    
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: input()
    _list = lambda: list(map(_type, _input()))
    
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H+offset*2, W+offset*2, intv)
    for i in range(offset, H+offset):
        row = _list()
        for j in range(offset, W+offset):
            grid[i][j] = row[j-offset]
    return grid

class MinCostFlow:
    """ 最小費用流(ダイクストラ版)：O(F*E*logV) """

    INF = 10**18

    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        self.pos = []

    def copy(self):
        res = MinCostFlow(self.N)
        res.G = [[a[:] for a in b] for b in self.G]
        res.pos = [a[:] for a in self.pos]
        return res

    def add_edge(self, fr, to, cap, cost):
        self.pos.append((fr, len(self.G[fr])))
        self.G[fr].append([to, cap, cost, len(self.G[to])])
        self.G[to].append([fr, 0, -cost, len(self.G[fr])-1])

    def flow(self, s, t, f):
        from heapq import heappush, heappop

        N = self.N; G = self.G
        INF = MinCostFlow.INF

        res = 0
        H = [0] * N
        prv_v = [0] * N
        prv_e = [0] * N

        while f:
            dist = [INF] * N
            dist[s] = 0
            que = [s]

            while que:
                c, v = divmod(heappop(que), N)
                if dist[v] < c:
                    continue
                for i, (to, cap, cost, _) in enumerate(G[v]):
                    if cap > 0 and dist[to] > dist[v] + cost + H[v] - H[to]:
                        dist[to] = r = dist[v] + cost + H[v] - H[to]
                        prv_v[to] = v; prv_e[to] = i
                        heappush(que, r*N+to)
            if dist[t] == INF:
                return INF

            for i in range(N):
                H[i] += dist[i]

            d = f; v = t
            while v != s:
                d = min(d, G[prv_v[v]][prv_e[v]][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = G[prv_v[v]][prv_e[v]]
                e[1] -= d
                G[v][e[3]][1] += d
                v = prv_v[v]
        return res

    def get_edge(self, i):
        e = self.G[self.pos[i][0]][self.pos[i][1]]
        re = self.G[e[0]][e[3]]
        
        return (self.pos[i][0], e[0], e[1]+re[1], re[1], e[2])

    def get_edges(self):
        M = len(self.pos)
        res = []
        for i in range(M):
            res.append(self.get_edge(i))
        return res
N, M = MAP()
grid = build_grid(N, M, '
N += 2
M += 2

cnt = 0
for i in range(1, N):
    for j in range(1, M):
        if grid[i][j] == 'o':
            cnt += 1
MX = cnt
mcf = MinCostFlow(N*M+2)
s = N*M
t = N*M+1
offset = 3000

directions = ((1, 0), (0, 1))
for i in range(1, N):
    for j in range(1, M):
        if grid[i][j] == 'o':
            
            mcf.add_edge(s, i*M+j, 1, offset)
        if grid[i][j] in ['o', '.']:
            
            mcf.add_edge(i*M+j, t, 1, offset)
            for di, dj in directions:
                ni, nj = i+di, j+dj
                if grid[ni][nj] in ['o', '.']:
                    
                    mcf.add_edge(i*M+j, ni*M+nj, MX, -1)

ans = offset*MX*2 - mcf.flow(s, t, MX)
print(ans)

Solution (Python):

import sys;input=sys.stdin.readline
from collections import deque, defaultdict
def encode(i, j):
    return 2 + K + i*M + j
from heapq import heappush, heappop
class MinCostFlow:
    INF = 10**18

    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def addEdge(self, fr, to, cap, cost):

        forward = [to, cap, cost, None]
        backward = forward[3] = [fr, 0, -cost, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def minCostFlow(self, s, t, f):
        N = self.N; G = self.G
        INF = MinCostFlow.INF

        res = 0
        H = [0]*N
        prv_v = [0]*N
        prv_e = [None]*N

        d0 = [INF]*N
        dist = [INF]*N

        while f:
            dist[:] = d0
            dist[s] = 0
            que = [(0, s)]

            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w, cap, cost, _ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prv_v[w] = v; prv_e[w] = e
                        heappush(que, (r, w))

            if dist[t] == INF:
                return None

            for i in range(N):
                H[i] += dist[i]

            d = f; v = t
            while v != s:
                d = min(d, prv_e[v][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prv_e[v]
                e[1] -= d
                e[3][1] += d
                v = prv_v[v]
        return res
N, M = map(int, input().split())
B = []
for _ in range(N):
    s = input().strip()
    B.append(s)

ks = []
for i in range(N):
    for j in range(M):
        if B[i][j] == "o":
            ks.append((i, j))
K = len(ks)
mcf = MinCostFlow(2+K+N*M)
for i in range(K):
    mcf.addEdge(0, i+2, 1, 0)

for ki in range(K):
    i, j = ks[ki]
    s = (i, j)
    queue=deque([s])
    vs = set([s])
    dist = defaultdict(int)
    while queue:
        u, v = queue.popleft()

        mcf.addEdge(2+ki, encode(u, v), 1, 10**5-dist[(u, v)])
        for nu, nv in [(u+1, v), (u, v+1)]:
            if (nu, nv) in vs:
                continue
            if nu >= N or nv >= M:
                continue
            if B[nu][nv] == "
                continue
            vs.add((nu, nv))
            queue.append((nu, nv))
            dist[(nu, nv)] = dist[(u, v)] + 1

for i in range(N):
    for j in range(M):
        mcf.addEdge(encode(i,j),1,1,0)

print(K*10**5-mcf.minCostFlow(0, 1, K))

Solution (Python):
N,M = map(int,input().split())
S = [input() for i in range(N)]

import heapq
class MinCostFlow:
    def __init__(self, n):
        self.n = n
        self.es = [[] for _ in range(n)] 
        self.pos = []

    def add_edge(self, fr, to, cap, cost):
        m = len(self.pos)
        self.pos.append((fr, len(self.es[fr])))
        self.es[fr].append([to, len(self.es[to]), cap, cost])
        self.es[to].append([fr, len(self.es[fr])-1, 0, -cost])
        return m

    def get_edges(self):
        ret = []
        for fr,i in self.pos:
            to,rev,cap,cost = self.es[fr][i]
            flow = self.es[to][rev][2]
            cap += flow
            ret.append((fr, to, cap, flow, cost))
        return ret

    def flow(self, s, t, flow_limit=2**63):
        return self.slope(s,t,flow_limit)[-1]

    def slope(self, s, t, flow_limit=2**63):
        self.dual = [0] * self.n
        self.dist = [2**63] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        self.vis = [False] * self.n
        def _dual_ref():
            self.dist = [2**63] * self.n
            self.pv = [-1] * self.n
            self.pe = [-1] * self.n
            self.vis = [False] * self.n
            hq = [(0,s)]
            self.dist[s] = 0
            while hq:
                p = heapq.heappop(hq)
                v = p[1]
                if self.vis[v]: continue
                self.vis[v] = True
                if v==t: break
                for i,(to,rev,cap,cost) in enumerate(self.es[v]):
                    if self.vis[to] or not cap: continue
                    _cost = cost - self.dual[to] + self.dual[v]
                    if self.dist[to] - self.dist[v] > _cost:
                        self.dist[to] = self.dist[v] + _cost
                        self.pv[to] = v
                        self.pe[to] = i
                        heapq.heappush(hq, (self.dist[to],to))
            if not self.vis[t]:
                return False
            for v in range(self.n):
                if not self.vis[v]: continue
                self.dual[v] -= self.dist[t] - self.dist[v]
            return True
        flow, cost, prev_cost = 0, 0, -1
        ret = [(flow, cost)]
        while flow < flow_limit:
            if not _dual_ref(): break
            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.es[self.pv[v]][self.pe[v]][2])
                v = self.pv[v]
            v = t
            while v != s:
                self.es[self.pv[v]][self.pe[v]][2] -= c
                self.es[v][self.es[self.pv[v]][self.pe[v]][1]][2] += c
                v = self.pv[v]
            d = -self.dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                ret.pop()
            ret.append((flow, cost))
            prev_cost = cost
        return ret

mf = MinCostFlow(N*M+2)
src,dst = N*M, N*M+1
big = 10000
for i in range(N):
    for j in range(M):
        if S[i][j] == 'o':
            mf.add_edge(src, i*M+j, 1, i+j)
for i in range(N):
    for j in range(M):
        if S[i][j] == '
        if i+1 < N and S[i+1][j] != '
            mf.add_edge(i*M+j, (i+1)*M+j, big, 0)
        if j+1 < M and S[i][j+1] != '
            mf.add_edge(i*M+j, i*M+j+1, big, 0)
for i in range(N):
    for j in range(M):
        if S[i][j] == '
        mf.add_edge(i*M+j, dst, 1, big-(i+j))

flow,cost = mf.flow(src,dst)
ans = flow*big - cost
print(ans)