Problem:
 Score : 100 points    Problem Statement You are given a grid of N rows and M columns. The square at the i -th row and j -th column will be denoted as (i,j) .
Some of the squares contain an object. All the remaining squares are empty.
The state of the grid is represented by strings S_1,S_2,\cdots,S_N . The square (i,j) contains an object if S_{i,j}=  # and is empty if S_{i,j}=  . .  Consider placing 1 \times 2 tiles on the grid. Tiles can be placed vertically or horizontally to cover two adjacent empty squares.
Tiles must not stick out of the grid, and no two different tiles may intersect. Tiles cannot occupy the square with an object.  Calculate the maximum number of tiles that can be placed and any configulation that acheives the maximum.      Constraints  1 \leq N \leq 100  1 \leq M \leq 100  S_i is a string with length M consists of # and . .         Input Input is given from Standard Input in the following format:  N  M  S_1  S_2  \vdots  S_N       Output On the first line, print the maximum number of tiles that can be placed.  On the next N lines, print a configulation that achieves the maximum.
Precisely, output the strings t_1,t_2,\cdots,t_N constructed by the following way.   t_i is initialized to S_i .  For each (i,j) , if there is a tile that occupies (i,j) and (i+1,j) , change t_{i,j} := v , t_{i+1,j} := ^ .  For each (i,j) , if there is a tile that occupies (i,j) and (i,j+1) , change t_{i,j} := > , t_{i,j+1} := < .   See samples for further information.  You may print any configulation that maximizes the number of tiles.        Sample Input 1 3 3
#..
..#
...      Sample Output 1 3
#><
vv#
^^.  The following output is also treated as a correct answer.  3
#><
v.#
^><

Solution (Python):
from typing import NamedTuple, Optional, List


class MaxFlow:
    class Edge(NamedTuple):
        src: int
        dst: int
        cap: int
        flow: int

    class _Edge:
        dst: int
        cap: int
        rev: Optional['MaxFlow._Edge']

        def __init__(self, dst: int, cap: int):
            self.dst = dst
            self.cap = cap
            self.rev = None

    def __init__(self, n: int):
        self._n = n
        self._g: List[List[MaxFlow._Edge]] = [[] for _ in range(n)]
        self._edges: List[MaxFlow._Edge] = []

    def add_edge(self, src: int, dst: int, cap: int) -> int:
        assert 0 <= src < self._n
        assert 0 <= dst < self._n
        assert 0 <= cap
        m = len(self._edges)
        e = MaxFlow._Edge(dst, cap)
        re = MaxFlow._Edge(src, 0)
        e.rev = re
        re.rev = e
        self._g[src].append(e)
        self._g[dst].append(re)
        self._edges.append(e)
        return m

    def get_edge(self, i: int) -> Edge:
        assert 0 <= i < len(self._edges)
        e = self._edges[i]
        re = e.rev
        return MaxFlow.Edge(
            re.dst,
            e.dst,
            e.cap + re.cap,
            re.cap
        )

    def edges(self) -> List[Edge]:
        return [self.get_edge(i) for i in range(len(self._edges))]

    def change_edge(self, i: int, new_cap: int, new_flow: int):
        assert 0 <= i < len(self._edges)
        assert 0 <= new_flow <= new_cap
        e = self._edges[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow

    def flow(self, s: int, t: int, flow_limit: Optional[int] = None) -> int:
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
        if flow_limit is None:
            flow_limit = sum(e.cap for e in self._g[s])

        current_edge = [0] * self._n
        level = [0] * self._n

        def fill(arr: List[int], value: int):
            for i in range(len(arr)):
                arr[i] = value

        def bfs() -> bool:
            fill(level, self._n)
            queue = []
            q_front = 0
            queue.append(s)
            level[s] = 0
            while q_front < len(queue):
                v = queue[q_front]
                q_front += 1
                next_level = level[v] + 1
                for e in self._g[v]:
                    if e.cap == 0 or level[e.dst] <= next_level:
                        continue
                    level[e.dst] = next_level
                    if e.dst == t:
                        return True
                    queue.append(e.dst)
            return False

        def dfs(lim) -> int:
            stack = []
            edge_stack = []
            stack.append(t)
            while stack:
                v = stack[-1]
                if v == s:
                    flow = min(lim, min(e.cap for e in edge_stack))
                    for e in edge_stack:
                        e.cap -= flow
                        e.rev.cap += flow
                    return flow
                next_level = level[v] - 1
                while current_edge[v] < len(self._g[v]):
                    e = self._g[v][current_edge[v]]
                    re = e.rev
                    if level[e.dst] != next_level or re.cap == 0:
                        current_edge[v] += 1
                        continue
                    stack.append(e.dst)
                    edge_stack.append(re)
                    break
                else:
                    stack.pop()
                    if edge_stack:
                        edge_stack.pop()
                    level[v] = self._n
            return 0

        flow = 0
        while flow < flow_limit:
            if not bfs():
                break
            fill(current_edge, 0)
            while flow < flow_limit:
                f = dfs(flow_limit - flow)
                flow += f
                if f == 0:
                    break
        return flow



def main() -> None:
    import sys
    n, m = map(int, sys.stdin.readline().split())
    s = n * m
    t = s + 1
    g = MaxFlow(t + 1)
    grid = [list(sys.stdin.readline().strip()) for _ in range(n)]

    def enc(i: int, j: int) -> int:
        return i * m + j

    def dec(v: int) -> (int, int):
        return v // m, v % m

    for i in range(n):
        for j in range(m):
            if grid[i][j] == '
                continue
            if (i + j) % 2 == 0:
                g.add_edge(s, enc(i, j), 1)
            else:
                g.add_edge(enc(i, j), t, 1)

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    for i in range(n):
        for j in range(m):
            if (i + j) % 2 == 1 or grid[i][j] == '
                continue
            for direction in range(4):
                ii = i + dx[direction]
                jj = j + dy[direction]
                if 0 <= ii < n and 0 <= jj < m and grid[ii][jj] == '.':
                    g.add_edge(enc(i, j), enc(ii, jj), 1)

    print(g.flow(s, t))
    for e in g.edges():
        if e.src == s or e.dst == t or e.flow == 0:
            continue
        (i, j) = dec(e.src)
        (ii, jj) = dec(e.dst)
        if i == ii + 1:
            grid[ii][jj] = 'v'
            grid[i][j] = '^'
        elif j == jj + 1:
            grid[ii][jj] = '>'
            grid[i][j] = '<'
        elif i == ii - 1:
            grid[i][j] = 'v'
            grid[ii][jj] = '^'
        else:
            grid[i][j] = '>'
            grid[ii][jj] = '<'

    for s in grid:
        print("".join(s))


if __name__ == '__main__':
    main()

Solution (Python):
from typing import NamedTuple, Optional, List


class MaxFlow:
    class Edge(NamedTuple):
        src: int
        dst: int
        cap: int
        flow: int

    class _Edge:
        dst: int
        cap: int
        rev: Optional['MaxFlow._Edge']

        def __init__(self, dst: int, cap: int):
            self.dst = dst
            self.cap = cap
            self.rev = None

    def __init__(self, n: int):
        self._n = n
        self._g: List[List[MaxFlow._Edge]] = [[] for _ in range(n)]
        self._edges: List[MaxFlow._Edge] = []

    def add_edge(self, src: int, dst: int, cap: int) -> int:
        assert 0 <= src < self._n
        assert 0 <= dst < self._n
        assert 0 <= cap
        m = len(self._edges)
        e = MaxFlow._Edge(dst, cap)
        re = MaxFlow._Edge(src, 0)
        e.rev = re
        re.rev = e
        self._g[src].append(e)
        self._g[dst].append(re)
        self._edges.append(e)
        return m

    def get_edge(self, i: int) -> Edge:
        assert 0 <= i < len(self._edges)
        e = self._edges[i]
        re = e.rev
        return MaxFlow.Edge(
            re.dst,
            e.dst,
            e.cap + re.cap,
            re.cap
        )

    def edges(self) -> List[Edge]:
        return [self.get_edge(i) for i in range(len(self._edges))]

    def change_edge(self, i: int, new_cap: int, new_flow: int):
        assert 0 <= i < len(self._edges)
        assert 0 <= new_flow <= new_cap
        e = self._edges[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow

    def flow(self, s: int, t: int, flow_limit: Optional[int] = None) -> int:
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
        if flow_limit is None:
            flow_limit = sum(e.cap for e in self._g[s])

        current_edge = [0] * self._n
        level = [0] * self._n

        def fill(arr: List[int], value: int):
            for i in range(len(arr)):
                arr[i] = value

        def bfs() -> bool:
            fill(level, self._n)
            queue = []
            q_front = 0
            queue.append(s)
            level[s] = 0
            while q_front < len(queue):
                v = queue[q_front]
                q_front += 1
                next_level = level[v] + 1
                for e in self._g[v]:
                    if e.cap == 0 or level[e.dst] <= next_level:
                        continue
                    level[e.dst] = next_level
                    if e.dst == t:
                        return True
                    queue.append(e.dst)
            return False

        def dfs(lim) -> int:
            stack = []
            edge_stack = []
            stack.append(t)
            while stack:
                v = stack[-1]
                if v == s:
                    flow = min(lim, min(e.cap for e in edge_stack))
                    for e in edge_stack:
                        e.cap -= flow
                        e.rev.cap += flow
                    return flow
                next_level = level[v] - 1
                while current_edge[v] < len(self._g[v]):
                    e = self._g[v][current_edge[v]]
                    re = e.rev
                    if level[e.dst] != next_level or re.cap == 0:
                        current_edge[v] += 1
                        continue
                    stack.append(e.dst)
                    edge_stack.append(re)
                    break
                else:
                    stack.pop()
                    if edge_stack:
                        edge_stack.pop()
                    level[v] = self._n
            return 0

        flow = 0
        while flow < flow_limit:
            if not bfs():
                break
            fill(current_edge, 0)
            while flow < flow_limit:
                f = dfs(flow_limit - flow)
                flow += f
                if f == 0:
                    break
        return flow



def main() -> None:
    import sys
    n, m = map(int, sys.stdin.readline().split())
    s = n * m
    t = s + 1
    g = MaxFlow(t + 1)
    grid = [list(sys.stdin.readline().strip()) for _ in range(n)]

    def enc(i: int, j: int) -> int:
        return i * m + j

    def dec(v: int) -> (int, int):
        return v // m, v % m

    for i in range(n):
        for j in range(m):
            if grid[i][j] == '
                continue
            if (i + j) % 2 == 0:
                g.add_edge(s, enc(i, j), 1)
            else:
                g.add_edge(enc(i, j), t, 1)

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    for i in range(n):
        for j in range(m):
            if (i + j) % 2 == 1 or grid[i][j] == '
                continue
            for direction in range(4):
                ii = i + dx[direction]
                jj = j + dy[direction]
                if 0 <= ii < n and 0 <= jj < m and grid[ii][jj] == '.':
                    g.add_edge(enc(i, j), enc(ii, jj), 1)

    print(g.flow(s, t))
    for e in g.edges():
        if e.src == s or e.dst == t or e.flow == 0:
            continue
        (i, j) = dec(e.src)
        (ii, jj) = dec(e.dst)
        if i == ii + 1:
            grid[ii][jj] = 'v'
            grid[i][j] = '^'
        elif j == jj + 1:
            grid[ii][jj] = '>'
            grid[i][j] = '<'
        elif i == ii - 1:
            grid[i][j] = 'v'
            grid[ii][jj] = '^'
        else:
            grid[i][j] = '>'
            grid[ii][jj] = '<'

    for s in grid:
        print("".join(s))


if __name__ == '__main__':
    main()

Solution (Python):
from typing import NamedTuple, Optional, List


class MaxFlow:
    class Edge(NamedTuple):
        src: int
        dst: int
        cap: int
        flow: int

    class _Edge:
        dst: int
        cap: int
        rev: Optional['MaxFlow._Edge']

        def __init__(self, dst: int, cap: int):
            self.dst = dst
            self.cap = cap
            self.rev = None

    def __init__(self, n: int):
        self._n = n
        self._g: List[List[MaxFlow._Edge]] = [[] for _ in range(n)]
        self._edges: List[MaxFlow._Edge] = []

    def add_edge(self, src: int, dst: int, cap: int) -> int:
        assert 0 <= src < self._n
        assert 0 <= dst < self._n
        assert 0 <= cap
        m = len(self._edges)
        e = MaxFlow._Edge(dst, cap)
        re = MaxFlow._Edge(src, 0)
        e.rev = re
        re.rev = e
        self._g[src].append(e)
        self._g[dst].append(re)
        self._edges.append(e)
        return m

    def get_edge(self, i: int) -> Edge:
        assert 0 <= i < len(self._edges)
        e = self._edges[i]
        re = e.rev
        return MaxFlow.Edge(
            re.dst,
            e.dst,
            e.cap + re.cap,
            re.cap
        )

    def edges(self) -> List[Edge]:
        return [self.get_edge(i) for i in range(len(self._edges))]

    def change_edge(self, i: int, new_cap: int, new_flow: int):
        assert 0 <= i < len(self._edges)
        assert 0 <= new_flow <= new_cap
        e = self._edges[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow

    def flow(self, s: int, t: int, flow_limit: Optional[int] = None) -> int:
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
        if flow_limit is None:
            flow_limit = sum(e.cap for e in self._g[s])

        current_edge = [0] * self._n
        level = [0] * self._n
        buf: List[int] = []
        edge_stack: List[MaxFlow._Edge] = []

        def fill(arr: List[int], value: int):
            for i in range(len(arr)):
                arr[i] = value

        def bfs() -> bool:
            fill(level, self._n)
            queue = buf
            queue.clear()
            q_front = 0
            queue.append(s)
            level[s] = 0
            while q_front < len(queue):
                v = queue[q_front]
                q_front += 1
                next_level = level[v] + 1
                for e in self._g[v]:
                    if e.cap == 0 or level[e.dst] <= next_level:
                        continue
                    level[e.dst] = next_level
                    if e.dst == t:
                        return True
                    queue.append(e.dst)
            return False

        def dfs(lim) -> int:
            stack = buf
            stack.clear()
            stack.append(t)
            edge_stack.clear()
            while stack:
                v = stack[-1]
                if v == s:
                    flow = min(lim, min(e.cap for e in edge_stack))
                    for e in edge_stack:
                        e.cap -= flow
                        e.rev.cap += flow
                    return flow
                next_level = level[v] - 1
                while current_edge[v] < len(self._g[v]):
                    e = self._g[v][current_edge[v]]
                    re = e.rev
                    if level[e.dst] != next_level or re.cap == 0:
                        current_edge[v] += 1
                        continue
                    stack.append(e.dst)
                    edge_stack.append(re)
                    break
                else:
                    stack.pop()
                    if edge_stack:
                        edge_stack.pop()
                    level[v] = self._n
            return 0

        flow = 0
        while flow < flow_limit:
            if not bfs():
                break
            fill(current_edge, 0)
            while flow < flow_limit:
                f = dfs(flow_limit - flow)
                flow += f
                if f == 0:
                    break
        return flow



def main() -> None:
    import sys
    n, m = map(int, sys.stdin.readline().split())
    s = n * m
    t = s + 1
    g = MaxFlow(t + 1)
    grid = [list(sys.stdin.readline().strip()) for _ in range(n)]

    def enc(i: int, j: int) -> int:
        return i * m + j

    def dec(v: int) -> (int, int):
        return v // m, v % m

    for i in range(n):
        for j in range(m):
            if grid[i][j] == '
                continue
            if (i + j) % 2 == 0:
                g.add_edge(s, enc(i, j), 1)
            else:
                g.add_edge(enc(i, j), t, 1)

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    for i in range(n):
        for j in range(m):
            if (i + j) % 2 == 1 or grid[i][j] == '
                continue
            for direction in range(4):
                ii = i + dx[direction]
                jj = j + dy[direction]
                if 0 <= ii < n and 0 <= jj < m and grid[ii][jj] == '.':
                    g.add_edge(enc(i, j), enc(ii, jj), 1)

    print(g.flow(s, t))
    for e in g.edges():
        if e.src == s or e.dst == t or e.flow == 0:
            continue
        (i, j) = dec(e.src)
        (ii, jj) = dec(e.dst)
        if i == ii + 1:
            grid[ii][jj] = 'v'
            grid[i][j] = '^'
        elif j == jj + 1:
            grid[ii][jj] = '>'
            grid[i][j] = '<'
        elif i == ii - 1:
            grid[i][j] = 'v'
            grid[ii][jj] = '^'
        else:
            grid[i][j] = '>'
            grid[ii][jj] = '<'

    for s in grid:
        print("".join(s))


if __name__ == '__main__':
    main()

Solution (Python):

NUMERIC_LIMITS = 10 ** 18
import queue
class maxFlow:
  class edge:
    def __init__(s, frm, to, cap, flow):
      s.frm, s.to = frm, to
      s.cap, s.flow = cap, flow

  def __init__(s, n):
    s._n = n
    s.g = [[] for _ in range(n)]
    s.pos = []

  def add_edge(s, frm, to, cap):
    m = len(s.pos)
    s.pos.append([frm, len(s.g[frm])])
    s.g[frm].append(s._edge(to, len(s.g[to]), cap))
    s.g[to].append(s._edge(frm,len(s.g[frm]) - 1, 0))
    return m

  def get_edge(s, i):
    m = len(s.pos)
    _e = s.g[s.pos[i][0]][s.pos[i][1]]
    _re = s.g[_e.to][_e.rev]
    return s.edge(s.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap)

  def edges(s):
    m = len(s.pos)
    result = []
    for i in range(m):
      result.append(s.get_edge(i))
    return result

  def change_edge(s, i, new_cap, new_flow):
    m = len(s.pos)
    _e = s.g[s.pos[i].to][s.pos[i].rev]
    _re = s.g[_e.to][_e.rev]
    _e.cap = new_cap - new_flow
    _re.cap = new_flow

  def flow(self, s, t, flow_limit = NUMERIC_LIMITS):
    level = [0] * self._n
    iter = [0] * self._n 

    def bfs():
      for i in range(self._n):
        level[i] = -1
      level[s] = 0
      que = queue.Queue()
      que.put(s)
      while not que.empty():
        v = que.get()
        for e in self.g[v]:
           if e.cap == 0 or level[e.to] >= 0: continue
           level[e.to] = level[v] + 1
           if e.to == t: return
           que.put(e.to)
    def dfs(this, v, up):
      if v == s: return up
      res = 0
      level_v = level[v]
      for i in range(iter[v], len(self.g[v])):
        e = self.g[v][i]
        if level_v <= level[e.to] or self.g[e.to][e.rev].cap == 0: continue
        d = this(this, e.to, min(up - res, self.g[e.to][e.rev].cap))
        if d <= 0: continue
        self.g[v][i].cap += d
        self.g[e.to][e.rev].cap -= d
        res += d
        if res == up: break
      return res

    flow = 0
    while flow < flow_limit:
      bfs()
      if level[t] == -1: break
      for i in range(self._n): iter[i]
      while flow < flow_limit:
        f = dfs(dfs, t, flow_limit - flow)
        if not f: break
        flow += f
    return flow

  def min_cut(self, s):
    visited = [False] * self._n
    que = queue.Queue()
    que.put(s)
    while not que.empty():
      p = que.get()
      visited[p] = True
      for e in self.g[p]:
        if e.cap and not visited[e.to]:
          visited[e.to] = True
          que.put(e.to)
    return visited

  class _edge:
    def __init__(s, to, rev, cap):
      s.to, s.rev = to, rev
      s.cap = cap




N, M = list(map(int, input().split()))
S = [list(input()) for _ in range(N)]

stop = "

flow = maxFlow(N * M + 2)
s = N * M
t = N * M + 1
for x in range(N):
  for y in range(M):
    if S[x][y] == stop: continue
    p = (x + y) % 2
    n = x * M + y
    if p:
      flow.add_edge(s, n, 1)
      for h, w in [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]]:
        if h < 0: continue
        if h >= N: continue
        if w < 0: continue
        if w >= M: continue
        if S[h][w] == stop: continue
        m = h * M + w
        flow.add_edge(n, m, 1)
    else:
      flow.add_edge(n, t, 1)

ans = flow.flow(s, t)

edges = flow.edges()
for edge in edges:
  if edge.flow == 0: continue
  if edge.frm == s: continue
  if edge.to == t: continue
  frm, to = edge.frm, edge.to
  fy, ty = frm % M, to % M
  fx, tx = (frm - fy) // M, (to - ty) // M
  if fx == tx:
    if fy > ty:
      fy, ty = ty, fy
    S[fx][fy] = ">"
    S[tx][ty] = "<"
  else:
    if fx > tx:
      fx, tx = tx, fx
    S[fx][fy] = "v"
    S[tx][ty] = "^"

print(ans)
for s in S:
  print("".join(s))

Solution (Python):





from collections import deque






import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


class Dinic:
    def __init__(self, v):
        self.V = v
        self.G = [list() for _ in range(v)]
        self.level = [0 for _ in range(v)]
        self.iter = [0 for _ in range(v)]
        self.pos = []

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


    def edges(self):
        es = []
        for f, t in self.pos:
            e = self.G[f][t]
            re = self.G[e[0]][e[2]]
            
            e = [f, e[0], e[1] + re[1], re[1]]
            es.append(e)
        return es

    
    def bfs(self, s):
        self.level = [-1 for _ in range(self.V)]
        self.level[s] = 0
        que = deque()
        que.append(s)
        while que:
            v = que.popleft()
            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if e[1] > 0 and self.level[e[0]] < 0:
                    self.level[e[0]] = self.level[v] + 1
                    que.append(e[0])

    
    def dfs(self, v, t, f):
        if v == t:
            return f
        for i in range(self.iter[v], len(self.G[v])):
            self.iter[v] = i
            e = self.G[v][i]
            if e[1] > 0 and self.level[v] < self.level[e[0]]:
                d = self.dfs(e[0], t, min(f, e[1]))
                if d > 0:
                    e[1] -= d
                    self.G[e[0]][e[2]][1] += d
                    return d

        return 0

    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            
            if self.level[t] < 0:
                return flow
            self.iter = [0 for _ in range(self.V)]
            f = self.dfs(s, t, INF)
            while f > 0:
                flow += f
                f = self.dfs(s, t, INF)


@mt
def slv(N, M, S):
    S = [list(s) for s in S]
    g = Dinic(N*M+2)
    s = N*M
    t = s + 1

    for i in range(N):
        for j in range(M):
            if S[i][j] == '
                continue
            v = i*M + j
            if (i + j )%2 == 0:
                g.add_edge(s, v, 1)
            else:
                g.add_edge(v, t, 1)



    for i in range(N):
        for j in range(M):
            if S[i][j] == '
                continue
            if (i+j) % 2 == 1:
                continue
            v0 = i*M + j
            for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
                if not (0 <= x < N and 0 <= y < M):
                    continue
                v1 = x*M + y
                g.add_edge(v0, v1, 1)

    print(g.max_flow(s, t))
    for from_, to, _, flow in g.edges():
        if from_ in {s, t} or to in {s, t} or flow == 0:
            continue
        i0 = from_ // M
        j0 = from_ % M
        i1 = to // M
        j1 = to % M

        if i0 == i1+1:
            S[i1][j1] = 'v'
            S[i0][j0] = '^'
        elif j0 == j1+1:
            S[i1][j1] = '>'
            S[i0][j0] = '<'
        elif i0 == i1-1:
            S[i1][j1] = '^'
            S[i0][j0] = 'v'
        else:
            S[i1][j1] = '<'
            S[i0][j0] = '>'

    for s in S:
        print(''.join(s))


def main():
    N, M = read_int_n()
    S = [read_str() for _ in range(N)]
    slv(N, M, S)


if __name__ == '__main__':
    main()

Solution (Python):

NUMERIC_LIMITS = 10 ** 18
import queue
class maxFlow:
  class edge:
    def __init__(s, frm, to, cap, flow):
      s.frm, s.to = frm, to
      s.cap, s.flow = cap, flow

  def __init__(s, n):
    s._n = n
    s.g = [[] for _ in range(n)]
    s.pos = []

  def add_edge(s, frm, to, cap):
    m = len(s.pos)
    s.pos.append([frm, len(s.g[frm])])
    s.g[frm].append(s._edge(to, len(s.g[to]), cap))
    s.g[to].append(s._edge(frm,len(s.g[frm]) - 1, 0))
    return m

  def get_edge(s, i):
    m = len(s.pos)
    _e = s.g[s.pos[i][0]][s.pos[i][1]]
    _re = s.g[_e.to][_e.rev]
    return s.edge(s.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap)

  def edges(s):
    m = len(s.pos)
    result = []
    for i in range(m):
      result.append(s.get_edge(i))
    return result

  def change_edge(s, i, new_cap, new_flow):
    m = len(s.pos)
    _e = s.g[s.pos[i].to][s.pos[i].rev]
    _re = s.g[_e.to][_e.rev]
    _e.cap = new_cap - new_flow
    _re.cap = new_flow

  def flow(self, s, t, flow_limit = NUMERIC_LIMITS):
    level = [0] * self._n
    iter = [0] * self._n 

    def bfs():
      for i in range(self._n):
        level[i] = -1
      level[s] = 0
      que = queue.Queue()
      que.put(s)
      while not que.empty():
        v = que.get()
        for e in self.g[v]:
           if e.cap == 0 or level[e.to] >= 0: continue
           level[e.to] = level[v] + 1
           if e.to == t: return
           que.put(e.to)
    def dfs(this, v, up):
      if v == s: return up
      res = 0
      level_v = level[v]
      for i in range(iter[v], len(self.g[v])):
        e = self.g[v][i]
        if level_v <= level[e.to] or self.g[e.to][e.rev].cap == 0: continue
        d = this(this, e.to, min(up - res, self.g[e.to][e.rev].cap))
        if d <= 0: continue
        self.g[v][i].cap += d
        self.g[e.to][e.rev].cap -= d
        res += d
        if res == up: break
      return res

    flow = 0
    while flow < flow_limit:
      bfs()
      if level[t] == -1: break
      for i in range(self._n): iter[i]
      while flow < flow_limit:
        f = dfs(dfs, t, flow_limit - flow)
        if not f: break
        flow += f
    return flow

  def min_cut(self, s):
    visited = [False] * self._n
    que = queue.Queue()
    que.put(s)
    while not que.empty():
      p = que.get()
      visited[p] = True
      for e in self.g[p]:
        if e.cap and not visited[e.to]:
          visited[e.to] = True
          que.put(e.to)
    return visited

  class _edge:
    def __init__(s, to, rev, cap):
      s.to, s.rev = to, rev
      s.cap = cap




N, M = list(map(int, input().split()))
S = [list(input()) for _ in range(N)]

stop = "

flow = maxFlow(N * M + 2)
s = N * M
t = N * M + 1
for x in range(N):
  for y in range(M):
    if S[x][y] == stop: continue
    p = (x + y) % 2
    n = x * M + y
    if p:
      flow.add_edge(s, n, 1)
      for h, w in [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]]:
        if h < 0: continue
        if h >= N: continue
        if w < 0: continue
        if w >= M: continue
        if S[h][w] == stop: continue
        m = h * M + w
        flow.add_edge(n, m, 1)
    else:
      flow.add_edge(n, t, 1)

ans = flow.flow(s, t)

edges = flow.edges()
for edge in edges:
  if edge.flow == 0: continue
  if edge.frm == s: continue
  if edge.to == t: continue
  frm, to = edge.frm, edge.to
  fy, ty = frm % M, to % M
  fx, tx = (frm - fy) // M, (to - ty) // M
  if fx == tx:
    if fy > ty:
      fy, ty = ty, fy
    S[fx][fy] = ">"
    S[tx][ty] = "<"
  else:
    if fx > tx:
      fx, tx = tx, fx
    S[fx][fy] = "v"
    S[tx][ty] = "^"

print(ans)
for s in S:
  print("".join(s))