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.#
^><



Sample input:
3 3
#..
..#
...


Sample output:
3
#><
vv#
^^.

Submission status: Runtime Error

Solution code:
from collections import deque
class mf_graph():
    n=1
    g=[[] for i in range(n)]
    pos=[]
    def __init__(self,N):
        self.n=N
        self.g=[[] for i in range(N)]
    def add_edge(self,f,t,cap):
        assert 0<=f<self.n, "0<=from<n,(from,n)=({0},{1})".format(f,self.n)
        assert 0<=t<self.n, "0<=to<n,(to,n)=({0},{1})".format(t,self.n)
        assert 0<=cap, "cap must be not negative,cap={0}".format(cap)
        m=len(self.pos)
        self.pos.append((f,len(self.g[f])))
        self.g[f].append((t,len(self.g[t]),cap))
        self.g[t].append((f,len(self.g[f])-1,0))
        return m
    def get_edge(self,i):
        m=len(self.pos)
        assert 0<=i<m, "0<=i<len(pos)=m,(i,m)=({0},{1})".format(i,m)
        e=self.g[self.pos[i][0]][self.pos[i][1]]
        re=self.g[e[0]][e[1]]
        return (self.pos[i][0],e[0],e[2]+re[2],re[2])
    def edges(self):
        m=len(self.pos)
        result=[]
        for i in range(m):
            result.append(self.get_edge(i))
        return result
    def change_edge(self,i,new_cap,new_flow):
        m=len(self.pos)
        assert 0<=i<m,"0<=i<len(pos)=m,(i,m)=({0},{1})".format(i,m)
        assert 0<=new_flow and new_flow<=new_cap, "0<=new_flow<=new_cap,(new_flow,new_cap)=({0},{1})".format(new_flow,new_cap)
        eto,erev,ecap=self.g[self.pos[i][0]][self.pos[i][1]]
        reto,rerev,recap=self.g[eto][erev]
        self.g[self.pos[i][0]][self.pos[i][1]]=(eto,erev,new_cap-new_flow)
        self.g[eto][erev]=(reto,rerev,new_flow)
    #edge : (from,to,cap,flow)
    #g in _edge
    #_edge : (to,rev,cap)
    def flow(self,s,t,flow_limit):
        assert 0<=s<self.n, "0<=s<n,(s,n)=({0},{1})".format(s,self.n)
        assert 0<=t<self.n, "0<=t<n,(t,n)=({0},{1})".format(t,self.n)
        level=[0 for i in range(self.n)]
        iter0=[0 for i in range(self.n)]
        que=deque([])
        def bfs():
            level=[-1 for i in range(self.n)]
            que=deque([])
            que.append(s)
            while(len(que)>0):
                v=que.popleft()
                for e in self.g[v]:
                    if e[2]==0 or level[e[0]]>=0:
                        continue
                    level[e[0]]=level[v]+1
                    if e[0]==t:
                        return
                    que.append(e[0])
        def dfs(v,up):
            if v==s:
                return up
            res=0
            level_v=level[v]
            for i in range(iter0[v],len(self.g[v])):
                e=self.g[v][i]
                if level_v<=level[e[0]] or self.g[e[0]][e[1]][2]==0:
                    continue
                d=dfs(e[0],min(up-res,self.g[e[0]][e[1]][2]))
                if d<=0:
                    continue
                self.g[v][i]=(self.g[v][i][0],self.g[v][i][1],self.g[v][i][2]+d)
                self.g[e[0]][e[1]]=(self.g[e[0]][e[1]][0],self.g[e[0]][e[1]][1],self.g[e[0]][e[1]][2]-d)
                res+=d
                if res==up:
                    break
            return res
        flow=0
        while(flow<flow_limit):
            bfs()
            if level[t]==-1:
                break
            iter0=[0 for i in range(self.n)]
            while(flow<flow_limit):
                f=dfs(t,flow_limit-flow)
                if not(f):
                    break
                flow+=f
            return flow
        
        def min_cut(s):
            visited=[False for i in range(self.n)]
            que=deque([])
            que.push(s)
            while(len(que)>0):
                p=que.popleft()
                visited[p]=True
                for e in g[p]:
                    if e[2] and not(visited[e[0]]):
                        visited[e[0]]=True
                        que.append(e[0])
            return visited

N,M=map(int,input().split())
S=[list(input()) for i in range(N)]
G=mf_graph(N*M+2)
D=[[(i*N+j+1) for j in range(M)] for i in range(N)]
di=[1,0,-1,0]
dj=[0,1,0,-1]
for i in range(N):
    for j in range(M):
        if S[i][j]=="#":
            continue
        if (i+j)%2==0:
            G.add_edge(0,i*N+j+1,1)
            for k in range(4):
                ni=i+di[k]
                nj=j+dj[k]
                if 0<=ni<N and 0<=nj<M:
                    if S[ni][nj]==".":
                        G.add_edge(i*N+j+1,ni*N+nj+1,1)
        else:
            G.add_edge(i*N+j+1,N*M+1,1)
G.flow(0,N*M+1,(1<<31)-1)
T=[[S[i][j] for j in range(M)] for i in range(N)]
for e in G.edges():
    if e[1]==N*M+1:
        continue
    if e[0]==0:
        continue
#    if e[3]==0:
#        continue
    ax=(e[0]-1)%N
    ay=(e[0]-1)//N
    bx=(e[1]-1)%N
    by=(e[1]-1)//N
    if ax==bx:
        if ay<by:
            T[by][bx]="^"
            T[ay][ax]="v"
        else:
            T[ay][ax]="^"
            T[by][bx]="v"
    else:
        if ax<bx:
            T[by][bx]="<"
            T[ay][ax]=">"
        else:
            T[ay][ax]="<"
            T[by][bx]=">"

for ans in T:
    print("".join(ans))

Submission status: Runtime Error

Solution code:
import sys
import itertools
import numpy as np
import networkx as nx

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = np.frombuffer(read(), 'S1').reshape(N, -1)[:, :M].astype('U1')

A = []
B = []
E = []

for i, j in itertools.product(range(N), range(M)):
    if S[i, j] == '#':
        continue
    elif (i + j) & 1:
        A.append((i, j))
    else:
        B.append((i, j))

for i, j in A:
    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        i1, j1 = i + dx, j + dy
        if 0 <= i1 < N and 0 <= j1 < M and S[i1, j1] != '#':
            E.append(((i, j), (i1, j1)))

G = nx.Graph()
G.add_nodes_from(A, bipartite=0)
G.add_nodes_from(B, bipartite=1)
G.add_edges_from(E)

M = nx.bipartite.eppstein_matching(G)
for key, item in M.items():
    i, j = key
    i1, j1 = item
    if i1 == i + 1:
        a, b = 'v', '^'
    elif i1 == i - 1:
        a, b = '^', 'v'
    elif j1 == j + 1:
        a, b = '>', '<'
    else:
        a, b = '<', '>'
    S[i, j], S[i1, j1] = a, b

print(len(M) // 2)
for row in S:
    print(''.join(row))

Submission status: Runtime Error

Solution code:
import sys
import itertools
import numpy as np
import networkx as nx

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = np.frombuffer(read(), 'S1').reshape(N, -1)[:, :M].astype('U1')

A = []
B = []
E = []

for i, j in itertools.product(range(N), range(M)):
    if S[i, j] == '#':
        continue
    elif (i + j) & 1:
        A.append((i, j))
    else:
        B.append((i, j))

for i, j in A:
    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        i1, j1 = i + dx, j + dy
        if 0 <= i1 < N and 0 <= j1 < M and S[i1, j1] != '#':
            E.append(((i, j), (i1, j1)))

G = nx.Graph()
G.add_nodes_from(A, bipartite=0)
G.add_nodes_from(B, bipartite=1)
G.add_edges_from(E)

M = nx.bipartite.hopcroft_karp_matching(G)
for key, item in M.items():
    i, j = key
    i1, j1 = item
    if i1 == i + 1:
        a, b = 'v', '^'
    elif i1 == i - 1:
        a, b = '^', 'v'
    elif j1 == j + 1:
        a, b = '>', '<'
    else:
        a, b = '<', '>'
    S[i, j], S[i1, j1] = a, b

print(len(M) // 2)
for row in S:
    print(''.join(row))

Submission status: Runtime Error

Solution code:
import sys
import itertools
import numpy as np
import networkx as nx

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = np.frombuffer(read(), 'S1').reshape(N, -1)[:, :M].astype('U1')

A = []
B = []
E = []

for i, j in itertools.product(range(N), range(M)):
    if S[i, j] == '#':
        continue
    elif (i + j) & 1:
        A.append((i, j))
    else:
        B.append((i, j))

for i, j in A:
    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        i1, j1 = i + dx, j + dy
        if 0 <= i1 < N and 0 <= j1 < M and S[i1, j1] != '#':
            E.append(((i, j), (i1, j1)))

G = nx.Graph()
G.add_nodes_from(A, bipartite=0)
G.add_nodes_from(B, bipartite=1)
G.add_edges_from(E)

M = nx.bipartite.maximum_matching(G)
for key, item in M.items():
    i, j = key
    i1, j1 = item
    if i1 == i + 1:
        a, b = 'v', '^'
    elif i1 == i - 1:
        a, b = '^', 'v'
    elif j1 == j + 1:
        a, b = '>', '<'
    else:
        a, b = '<', '>'
    S[i, j], S[i1, j1] = a, b

print(len(M) // 2)
for row in S:
    print(''.join(row))

Submission status: Runtime Error

Solution code:
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()) for _ in range(n)]

mf = MaxFlow(n * m + 2)

start = n * m
end = n * m + 1
d = [(1,0), (-1,0), (0,1), (0,-1)]

for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        else:
            if (i + j) % 2 == 0:
                mf.add_edge(start, i * m + j, 1)

                for i1, j1 in d:
                    if 0 <= i + i1 < n and 0 <= j + j1 < m:
                        if s[i+i1][j+j1] == '.':
                            mf.add_edge(i * m + j, (i+i1) * m + (j+j1), 1)

            else:
                mf.add_edge(i * m + j, end, 1)

print(mf.max_flow(start, end))

for i in range(len(mf.pos)):
    s1, e1, cap, flow = mf.get_edge(i)
    if flow == 0 or s1 == start or e1 == end:
        continue
    else:
        si = s1 // n
        sj = s1 % n
        ei = e1 // n
        ej = e1 % n
        if s1 - e1 == 1:
            s[si][sj] = '<'
            s[ei][ej] = '>'
        elif e1 - s1 == 1:
            s[si][sj] = '>'
            s[ei][ej] = '<'
        elif s1 - e1 == m:
            s[si][sj] = '^'
            s[ei][ej] = 'v'
        else:
            s[si][sj] = 'v'
            s[ei][ej] = '^'

for i in s:
    for j in i:
        print(j, end = '')
    print()

Submission status: Runtime Error

Solution code:
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()) for _ in range(n)]

mf = MaxFlow(n * m + 2)

start = n * m
end = n * m + 1
d = [(1,0), (-1,0), (0,1), (0,-1)]

for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        else:
            if (i + j) % 2 == 0:
                mf.add_edge(start, i * n + j, 1)

                for i1, j1 in d:
                    if 0 <= i + i1 < n and 0 <= j + j1 < m:
                        if s[i+i1][j+j1] == '.':
                            mf.add_edge(i * n + j, (i+i1) * n + (j+j1), 1)

            else:
                mf.add_edge(i * n + j, end, 1)

print(mf.max_flow(start, end))

for i in range(len(mf.pos)):
    s1, e1, cap, flow = mf.get_edge(i)
    if flow == 0 or s1 == start or e1 == end:
        continue
    else:
        si = s1 // n
        sj = s1 % n
        ei = e1 // n
        ej = e1 % n
        if s1 - e1 == 1:
            s[si][sj] = '<'
            s[ei][ej] = '>'
        elif e1 - s1 == 1:
            s[si][sj] = '>'
            s[ei][ej] = '<'
        elif s1 - e1 == m:
            s[si][sj] = '^'
            s[ei][ej] = 'v'
        else:
            s[si][sj] = 'v'
            s[ei][ej] = '^'

for i in s:
    for j in i:
        print(j, end = '')
    print()

Submission status: Runtime Error

Solution code:
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()) for _ in range(n)]

mf = MaxFlow(n * m + 2)

start = n * m
end = n * m + 1
d = [(1,0), (-1,0), (0,1), (0,-1)]

for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        else:
            if (i + j) % 2 == 0:
                mf.add_edge(start, i * n + j, 1)

                for i1, j1 in d:
                    if 0 <= i + i1 < n and 0 <= j + j1 < m:
                        if s[i+i1][j+j1] == '.':
                            mf.add_edge(i * n + j, (i+i1) * n + (j+j1), 1)

            else:
                mf.add_edge(i * n + j, end, 1)

print(mf.max_flow(start, end))

for i in range(n*m+2):
    s1, e1, cap, flow = mf.get_edge(i)
    if flow == 0 or s1 == start or e1 == end:
        continue
    else:
        si = s1 // n
        sj = s1 % n
        ei = e1 // n
        ej = e1 % n
        if s1 - e1 == 1:
            s[si][sj] = '<'
            s[ei][ej] = '>'
        elif e1 - s1 == 1:
            s[si][sj] = '>'
            s[ei][ej] = '<'
        elif s1 - e1 == m:
            s[si][sj] = '^'
            s[ei][ej] = 'v'
        else:
            s[si][sj] = 'v'
            s[ei][ej] = '^'

for i in s:
    for j in i:
        print(j, end = '')
    print()

Submission status: Runtime Error

Solution code:
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()) for _ in range(n)]

mf = MaxFlow(n * m + 2)

start = n * m
end = n * m + 1
d = [(1,0), (-1,0), (0,1), (0,-1)]

for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        else:
            if (i + j) % 2 == 0:
                mf.add_edge(start, i * n + j, 1)

                for i1, j1 in d:
                    if 0 <= i + i1 < n and 0 <= j + j1 < m:
                        if s[i+i1][j+j1] == '.':
                            mf.add_edge(i * n + j, (i+i1) * n + (j+j1), 1)

            else:
                mf.add_edge(i * n + j, end, 1)

print(mf.max_flow(start, end))

for i in range(n*m+2):
    s1, e1, cap, flow = mf.get_edge(i)
    if flow == 0 or s1 == start or e1 == end:
        continue
    else:
        print(s1, e1)
        si = s1 // n
        sj = s1 % n
        ei = e1 // n
        ej = e1 % n
        if s1 - e1 == 1:
            s[si][sj] = '<'
            s[ei][ej] = '>'
        elif e1 - s1 == 1:
            s[si][sj] = '>'
            s[ei][ej] = '<'
        elif s1 - e1 == m:
            s[si][sj] = '^'
            s[ei][ej] = 'v'
        else:
            s[si][sj] = 'v'
            s[ei][ej] = '^'

for i in s:
    for j in i:
        print(j, end = '')
    print()