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):
import cProfile




from collections import deque


class mf_graph:

    

    def __init__(self, N):
        self.N = N
        self.graph = [[] for _ in range(N)]
        self.capacities = [dict() for _ in range(N)]

    def add_edge(self, v, w, cap):
        self.graph[v].append(w)
        self.graph[w].append(v)
        self.capacities[v][w] = cap
        self.capacities[w][v] = 0

    def bfs(self, s, t):
        self.level = [-1] * self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            if v == t:
                return True
            for w, cap in self.capacities[v].items():
                if cap and self.level[w] == -1:
                    self.level[w] = self.level[v] + 1
                    q.append(w)
        return False

    def dfs(self, s, t, up):
        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    v = self.graph[w][self.it[w]]
                    flow = min(flow, self.capacities[v][w])
                for w in st:
                    v = self.graph[w][self.it[w]]
                    self.capacities[w][v] += flow
                    self.capacities[v][w] -= flow
                return flow
            while self.it[v] < len(self.graph[v]):
                w = self.graph[v][self.it[v]]
                cap = self.capacities[w][v]
                if cap and self.level[w] != -1:
                    if self.level[v] > self.level[w]:
                        st.append(w)
                        break
                self.it[v] += 1
            else:
                st.pop()
                self.level[v] = self.N
        return 0

    def flow(self, s, t, flow_limit=float('inf')):
        flow = 0
        while flow < flow_limit and self.bfs(s, t):
            self.it = [0]*self.N
            while flow < flow_limit:
                f = self.dfs(s, t, flow_limit - flow)
                if not f:
                    break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [False]*self.N
        q = deque([s])
        while q:
            v = q.pop()
            visited[v] = True
            for w, cap in self.capacities[v].items():
                if cap and not visited[w]:
                    q.append(w)


N, M = map(int, input().split())
S = [list(input()) for i in range(N)]
"""
with open("flow.txt") as f:
    N, M = map(int, f.readline().rstrip().split())
    S = [list(f.readline().rstrip()) for i in range(N)]

dc = mf_graph(N*M+2)

s = N*M
t = N*M+1
for i in range(N):
    for j in range(M):
        if (i+j) % 2 == 0:
            dc.add_edge(s, M*i+j, 1)
        else:
            dc.add_edge(M*i+j, t, 1)

for i in range(N):
    for j in range(M):
        if j+1 < M and S[i][j] == "." and S[i][j+1] == ".":
            u, v = M*i+j, M*i+j+1
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)
        if i+1 < N and S[i][j] == "." and S[i + 1][j] == ".":
            u, v = M*i+j, M*(i+1)+j
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)

print(dc.flow(s, t))
for u in range(N*M+2):
    ui, uj = divmod(u, M)
    if (ui+uj) % 2 == 1:
        continue
    for v, cap in dc.capacities[u].items():
        if cap != 0:
            continue
        vi, vj = divmod(v, M)
        if u != s and u != t and v != s and v != t:
            if ui+1 == vi:
                S[ui][uj] = "v"
                S[vi][vj] = "^"
            elif ui == vi+1:
                S[ui][uj] = "^"
                S[vi][vj] = "v"
            elif uj+1 == vj:
                S[ui][uj] = ">"
                S[vi][vj] = "<"
            elif uj == vj+1:
                S[ui][uj] = "<"
                S[vi][vj] = ">"

for res in S:
    print("".join(res))

Solution (Python):
from collections import deque


class MaxFlow:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.pos = []

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

    def get_edge(self, idx):
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap = self.graph[to][rev]
        return rev_to, to, cap + rev_cap, rev_cap

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.get_edge(i)

    def change_edge(self, idx, new_cap, new_flow):
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        self.graph[self.pos[idx][0]][self.pos[idx][1]][2] = new_cap - new_flow
        self.graph[to][rev][2] = new_flow

    def dfs(self, s, v, up):
        if v == s:
            return up
        res = 0
        lv = self.level[v]
        for i in range(self.iter[v], len(self.graph[v])):
            to, rev, cap = self.graph[v][i]
            if lv <= self.level[to] or self.graph[to][rev][2] == 0:
                continue
            d = self.dfs(s, to, min(up - res, self.graph[to][rev][2]))
            if d <= 0:
                continue
            self.graph[v][i][2] += d
            self.graph[to][rev][2] -= d
            res += d
            if res == up:
                break
            self.iter[v] += 1
        return res

    def max_flow(self, s, t):
        return self.max_flow_with_limit(s, t, 2 ** 63 - 1)

    def max_flow_with_limit(self, s, t, limit):
        flow = 0
        while flow < limit:
            self.level = [-1] * self.n
            self.level[s] = 0
            queue = deque()
            queue.append(s)
            while queue:
                v = queue.popleft()
                for to, rev, cap in self.graph[v]:
                    if cap == 0 or self.level[to] >= 0:
                        continue
                    self.level[to] = self.level[v] + 1
                    if to == t:
                        break
                    queue.append(to)
            if self.level[t] == -1:
                break
            self.iter = [0] * self.n
            while flow < limit:
                f = self.dfs(s, t, limit - flow)
                if not f:
                    break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [0] * self.n
        queue = deque()
        queue.append(s)
        while queue:
            p = queue.popleft()
            visited[p] = True
            for to, rev, cap in self.graph[p]:
                if cap and not visited[to]:
                    visited[to] = True
                    queue.append(to)
        return visited


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

mf = MaxFlow(N * M + 2)

start = N * M
end = N * M + 1

for i in range(N):
    for j in range(M):
        if S[i][j] == '
            continue

        if (i + j) % 2 == 0:
            mf.add_edge(start, i * M + j, 1)

            for i2, j2 in [(i, j+1), (i, j-1), (i+1, j), (i-1, j)]:
                if 0 <= i2 < N and 0 <= j2 < M and S[i2][j2] != '
                    mf.add_edge(i * M + j, i2 * M + j2, 1)
        else:
            mf.add_edge(i * M + j, end, 1)

print(mf.max_flow(start, end))

for fr, to, cap, flow in mf.edges():
    if flow == 0 or fr == start or to == end:
        continue

    fi, fj = divmod(fr, M)
    ti, tj = divmod(to, M)

    if fi < ti:
        S[fi][fj] = 'v'
        S[ti][tj] = '^'
    elif fi > ti:
        S[fi][fj] = '^'
        S[ti][tj] = 'v'
    elif fj < tj:
        S[fi][fj] = '>'
        S[ti][tj] = '<'
    else:
        S[fi][fj] = '<'
        S[ti][tj] = '>'

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

Solution (Python):
import sys
input = sys.stdin.readline

N,M=map(int,input().split())
MAP=[list(input().strip()) for i in range(N)]

start=N*M+2
goal=N*M+3

EDGE=[set() for i in range(N*M+5)]

for i in range(N):
    for j in range(M):
        if (i+j)%2==0 and MAP[i][j]==".":
            EDGE[start].add(i*M+j)

            for x,y in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
                if 0<=x<N and 0<=y<M and MAP[x][y]==".":
                    EDGE[i*M+j].add(x*M+y)

        if (i+j)%2==1 and MAP[i][j]==".":
            EDGE[i*M+j].add(goal)

count=0 

while True:

    USED=[0]*(N*M+5)
    ROUTE=[-1]*(N*M+5)
    Q=[start]
    
    while Q: 
        NOW=Q.pop()
        if NOW==goal:
            break
        
        for to in EDGE[NOW]:
            if USED[to]==0:
                ROUTE[to]=NOW 
                USED[to]=1
                Q.append(to)
    else:
        break

    count+=1
    i=goal
    while i!=start: 
        j=ROUTE[i]
        EDGE[j].remove(i) 
        EDGE[i].add(j) 
        i=j
        
print(count)

for i in range(N):
    for j in range(M):
        if (i+j)%2==1:
            for to in EDGE[i*M+j]:
                if M>1 and to == i*M+j-1:
                    MAP[i][j]="<"
                    MAP[i][j-1]=">"
                elif M>1 and to == i*M+j+1:
                    MAP[i][j]=">"
                    MAP[i][j+1]="<"
                elif to == i*M+j-M:
                    MAP[i][j]="^"
                    MAP[i-1][j]="v"
                elif to == i*M+j+M:
                    MAP[i][j]="v"
                    MAP[i+1][j]="^"

for m in MAP:
    print("".join(m))

Solution (Python):
from collections import deque


class MF_graph(object):
    def __init__(self, n):
        self.n = n
        self.g = [[] for _ in range(n)]  
        self.pos = []

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

    def get_edge(self, i):
        e_to, e_rev, e_cap = self.g[self.pos[i][0]][self.pos[i][1]]
        re_to, _, re_cap = self.g[e_to][e_rev]
        
        return (re_to, e_to, e_cap + re_cap, re_cap)

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.get_edge(i)

    def change_edge(self, i, new_cap, new_flow):
        f, s = self.pos[i]
        rf, rs, _ = self.g[f][s]
        self.g[f][s][2] = new_cap - new_flow
        self.g[rf][rs][2] = new_flow
        return

    def dfs(self, s, v, up):
        if v == s:
            return up
        res = 0
        level_v = self.level[v]
        for i in range(self.iter[v], len(self.g[v])):
            u_to, u_rev, _ = self.g[v][i]
            if level_v <= self.level[u_to] or self.g[u_to][u_rev][2] == 0:
                continue
            d = self.dfs(s, u_to, min(up - res, self.g[u_to][u_rev][2]))
            if d <= 0:
                continue
            self.g[v][i][2] += d
            self.g[u_to][u_rev][2] -= d
            res += d
            if res == up:
                break
        return res

    def flow(self, s, t, flow_limit=10**18):
        self.iter = [0] * self.n

        flow = 0
        while flow < flow_limit:
            self.level = [-1] * self.n
            self.level[s] = 0
            que = deque([s])
            while que:
                v = que.popleft()
                for u_to, _, u_cap in self.g[v]:
                    if u_cap == 0 or self.level[u_to] >= 0:
                        continue
                    self.level[u_to] = self.level[v] + 1
                    if u_to == t:
                        break
                    que.append(u_to)

            if self.level[t] == -1:
                break
            self.iter = [0] * self.n
            while flow < flow_limit:
                f = self.dfs(s, t, flow_limit - flow)
                if not f:
                    break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [False] * self.n
        que = deque([s])
        while que:
            v = que.popleft()
            visited[v] = True
            for u_to, _, u_cap in self.g[v]:
                if u_cap and (not visited[u_to]):
                    visited[u_to] = True
                    que.append(u_to)
        return visited


if __name__ == "__main__":
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10 ** 7)

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

    g = MF_graph(N * M + 2)
    source = N * M
    sink = N * M + 1

    for i in range(N):
        for j in range(M):
            if grid[i][j] == "
                continue
            v = i * M + j
            if (i + j) % 2 == 0:
                g.add_edge(source, v, 1)
            else:
                g.add_edge(v, sink, 1)

    for i in range(N):
        for j in range(M):
            if (i + j) % 2 or grid[i][j] == "
                continue
            v0 = i * M + j
            if i and grid[i - 1][j] == ".":
                v1 = (i - 1) * M + j
                g.add_edge(v0, v1, 1)
            if j and grid[i][j - 1] == ".":
                v1 = i * M + j - 1
                g.add_edge(v0, v1, 1)
            if i + 1 < N and grid[i + 1][j] == ".":
                v1 = (i + 1) * M + j
                g.add_edge(v0, v1, 1)
            if j + 1 < M and grid[i][j + 1] == ".":
                v1 = i * M + j + 1
                g.add_edge(v0, v1, 1)

    res = g.flow(source, sink)

    for frm, to, cap, flow in g.edges():
        if frm == source or to == sink or flow == 0:
            continue
        i0, j0 = divmod(frm, M)
        i1, j1 = divmod(to, M)
        if i0 == i1 + 1:
            grid[i1][j1] = "v"
            grid[i0][j0] = "^"
        elif j0 == j1 + 1:
            grid[i1][j1] = ">"
            grid[i0][j0] = "<"
        elif i0 == i1 - 1:
            grid[i1][j1] = "^"
            grid[i0][j0] = "v"
        else:
            grid[i1][j1] = "<"
            grid[i0][j0] = ">"

    print(res)
    for g in grid:
        print("".join(g))

Solution (Python):
from collections import deque

class MaxFlow():
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.pos = []

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

    def get_edge(self, idx):
        
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap = self.graph[to][rev]
        return rev_to, to, cap + rev_cap, rev_cap

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.get_edge(i)

    def change_edge(self, idx, new_cap, new_flow):
        
        
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        self.graph[self.pos[idx][0]][self.pos[idx][1]][2] = new_cap - new_flow
        self.graph[to][rev][2] = new_flow

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.level[v]
        for i in range(self.iter[v], len(self.graph[v])):
            to, rev, cap = self.graph[v][i]
            if lv <= self.level[to] or self.graph[to][rev][2] == 0: continue
            d = self.dfs(s, to, min(up - res, self.graph[to][rev][2]))
            if d <= 0: continue
            self.graph[v][i][2] += d
            self.graph[to][rev][2] -= d
            res += d
            if res == up: break
            self.iter[v] += 1
        return res

    def max_flow(self, s, t):
        return self.max_flow_with_limit(s, t, 2**63 - 1)

    def max_flow_with_limit(self, s, t, limit):
        
        
        flow = 0
        while flow < limit:
            self.level = [-1] * self.n
            self.level[s] = 0
            queue = deque()
            queue.append(s)
            while queue:
                v = queue.popleft()
                for to, rev, cap in self.graph[v]:
                    if cap == 0 or self.level[to] >= 0: continue
                    self.level[to] = self.level[v] + 1
                    if to == t: break
                    queue.append(to)
            if self.level[t] == -1: break
            self.iter = [0] * self.n
            while flow < limit:
                f = self.dfs(s, t, limit - flow)
                if not f: break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [0] * self.n
        queue = deque()
        queue.append(s)
        while queue:
            p = queue.popleft()
            visited[p] = True
            for to, rev, cap in self.graph[p]:
                if cap and not visited[to]:
                    visited[to] = True
                    queue.append(to)
        return visited




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

mf = MaxFlow(N * M + 2)
s = N * M
t = N * M + 1

for i in range(N):
    for j in range(M):
        if S[i][j] == '
            continue
        if (i + j) % 2 == 0:
            mf.add_edge(s, i * M + j, 1)
            for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
                ni = i + di
                nj = j + dj
                if 0 <= ni < N and 0 <= nj < M and S[ni][nj] == '.':
                    mf.add_edge(i * M + j, ni * M + nj, 1)
        else:
            mf.add_edge(i * M + j, t, 1)

print(mf.max_flow(s, t))

for fr, to, cap, flow in mf.edges():
    if flow == 0 or fr == s or to == t:
        continue
    si, sj = divmod(fr, M)
    ti, tj = divmod(to, M)
    if si < ti:
        S[si][sj] = 'v'
        S[ti][tj] = '^'
    elif sj < tj:
        S[si][sj] = '>'
        S[ti][tj] = '<'
    elif si > ti:
        S[si][sj] = '^'
        S[ti][tj] = 'v'
    else:
        S[si][sj] = '<'
        S[ti][tj] = '>'

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

Solution (Python):
from collections import deque

class MaxFlow():
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.pos = []

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

    def get_edge(self, idx):
        
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap = self.graph[to][rev]
        return rev_to, to, cap + rev_cap, rev_cap

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.get_edge(i)

    def change_edge(self, idx, new_cap, new_flow):
        
        
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        self.graph[self.pos[idx][0]][self.pos[idx][1]][2] = new_cap - new_flow
        self.graph[to][rev][2] = new_flow

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.level[v]
        for i in range(self.iter[v], len(self.graph[v])):
            to, rev, cap = self.graph[v][i]
            if lv <= self.level[to] or self.graph[to][rev][2] == 0: continue
            d = self.dfs(s, to, min(up - res, self.graph[to][rev][2]))
            if d <= 0: continue
            self.graph[v][i][2] += d
            self.graph[to][rev][2] -= d
            res += d
            if res == up: break
            self.iter[v] += 1
        return res

    def max_flow(self, s, t):
        return self.max_flow_with_limit(s, t, 2**63 - 1)

    def max_flow_with_limit(self, s, t, limit):
        
        
        flow = 0
        while flow < limit:
            self.level = [-1] * self.n
            self.level[s] = 0
            queue = deque()
            queue.append(s)
            while queue:
                v = queue.popleft()
                for to, rev, cap in self.graph[v]:
                    if cap == 0 or self.level[to] >= 0: continue
                    self.level[to] = self.level[v] + 1
                    if to == t: break
                    queue.append(to)
            if self.level[t] == -1: break
            self.iter = [0] * self.n
            while flow < limit:
                f = self.dfs(s, t, limit - flow)
                if not f: break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [0] * self.n
        queue = deque()
        queue.append(s)
        while queue:
            p = queue.popleft()
            visited[p] = True
            for to, rev, cap in self.graph[p]:
                if cap and not visited[to]:
                    visited[to] = True
                    queue.append(to)
        return visited

import sys
input = sys.stdin.readline

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

mf = MaxFlow(N * M + 2)
s = N * M
t = N * M + 1

for i in range(N):
    for j in range(M):
        if S[i][j] == '
            continue
        if (i + j) % 2 == 0:
            mf.add_edge(s, i * M + j, 1)
            for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
                ni = i + di
                nj = j + dj
                if 0 <= ni < N and 0 <= nj < M and S[ni][nj] == '.':
                    mf.add_edge(i * M + j, ni * M + nj, 1)
        else:
            mf.add_edge(i * M + j, t, 1)

print(mf.max_flow(s, t))

for fr, to, cap, flow in mf.edges():
    if flow == 0 or fr == s or to == t:
        continue
    si, sj = divmod(fr, M)
    ti, tj = divmod(to, M)
    if si < ti:
        S[si][sj] = 'v'
        S[ti][tj] = '^'
    elif sj < tj:
        S[si][sj] = '>'
        S[ti][tj] = '<'
    elif si > ti:
        S[si][sj] = '^'
        S[ti][tj] = 'v'
    else:
        S[si][sj] = '<'
        S[ti][tj] = '>'

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

Solution (Python):
from collections import deque

class MaxFlow():
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.pos = []

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

    def get_edge(self, idx):
        
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap = self.graph[to][rev]
        return rev_to, to, cap + rev_cap, rev_cap

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.get_edge(i)

    def change_edge(self, idx, new_cap, new_flow):
        
        
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        self.graph[self.pos[idx][0]][self.pos[idx][1]][2] = new_cap - new_flow
        self.graph[to][rev][2] = new_flow

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.level[v]
        for i in range(self.iter[v], len(self.graph[v])):
            to, rev, cap = self.graph[v][i]
            if lv <= self.level[to] or self.graph[to][rev][2] == 0: continue
            d = self.dfs(s, to, min(up - res, self.graph[to][rev][2]))
            if d <= 0: continue
            self.graph[v][i][2] += d
            self.graph[to][rev][2] -= d
            res += d
            if res == up: break
            self.iter[v] += 1
        return res

    def max_flow(self, s, t):
        return self.max_flow_with_limit(s, t, 2**63 - 1)

    def max_flow_with_limit(self, s, t, limit):
        
        
        flow = 0
        while flow < limit:
            self.level = [-1] * self.n
            self.level[s] = 0
            queue = deque()
            queue.append(s)
            while queue:
                v = queue.popleft()
                for to, rev, cap in self.graph[v]:
                    if cap == 0 or self.level[to] >= 0: continue
                    self.level[to] = self.level[v] + 1
                    if to == t: break
                    queue.append(to)
            if self.level[t] == -1: break
            self.iter = [0] * self.n
            while flow < limit:
                f = self.dfs(s, t, limit - flow)
                if not f: break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [0] * self.n
        queue = deque()
        queue.append(s)
        while queue:
            p = queue.popleft()
            visited[p] = True
            for to, rev, cap in self.graph[p]:
                if cap and not visited[to]:
                    visited[to] = True
                    queue.append(to)
        return visited

import sys
input = sys.stdin.readline

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

mf = MaxFlow(N * M + 2)

for i in range(N):
    for j in range(M):
        if S[i][j] == '
            continue
        if (i + j) % 2 == 0:
            mf.add_edge(N * M, i * M + j, 1)
            for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
                ni = i + di
                nj = j + dj
                if 0 <= ni < N and 0 <= nj < M and S[ni][nj] == '.':
                    mf.add_edge(i * M + j, ni * M + nj, 1)
        else:
            mf.add_edge(i * M + j, N * M + 1, 1)

print(mf.max_flow(M * N, M * N + 1))

for fr, to, cap, flow in mf.edges():
    if flow == 0 or fr == N * M or to == N * M + 1:
        continue
    si, sj = divmod(fr, M)
    ti, tj = divmod(to, M)
    if si < ti:
        S[si][sj] = 'v'
        S[ti][tj] = '^'
    elif sj < tj:
        S[si][sj] = '>'
        S[ti][tj] = '<'
    elif si > ti:
        S[si][sj] = '^'
        S[ti][tj] = 'v'
    else:
        S[si][sj] = '<'
        S[ti][tj] = '>'

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