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 in Haskell (GHC 8.8.3) (judge verdict: Accepted)

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns, BinaryLiterals, CPP, DerivingStrategies  #-}
{-# LANGUAGE DerivingVia, FlexibleContexts, FlexibleInstances       #-}
{-# LANGUAGE GeneralizedNewtypeDeriving, KindSignatures, LambdaCase #-}
{-# LANGUAGE MagicHash, MultiParamTypeClasses, MultiWayIf           #-}
{-# LANGUAGE NumericUnderscores, OverloadedStrings, PatternSynonyms #-}
{-# LANGUAGE RankNTypes, RecordWildCards, ScopedTypeVariables       #-}
{-# LANGUAGE StandaloneDeriving, TupleSections, TypeApplications    #-}
{-# LANGUAGE TypeFamilies, TypeInType, UnboxedTuples, ViewPatterns  #-}

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.Reader
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bifunctor
import           Data.Bool
import qualified Data.ByteString                   as B
import qualified Data.ByteString.Builder           as B
import qualified Data.ByteString.Char8             as C
import qualified Data.ByteString.Internal          as B
import qualified Data.ByteString.Unsafe            as B
import           Data.Char
import qualified Data.Foldable                     as F
import           Data.Function
import           Data.Functor.Identity
import qualified Data.IntMap.Strict                as IM
import qualified Data.IntSet                       as IS
import qualified Data.List                         as L
import qualified Data.Map.Strict                   as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive
import           Data.Proxy
import           Data.Ratio
import           Data.Semigroup
import qualified Data.Set                          as S
import           Data.Tuple
import qualified Data.Vector                       as V
import qualified Data.Vector.Algorithms.Intro      as Intro
import qualified Data.Vector.Fusion.Stream.Monadic as MS
import qualified Data.Vector.Fusion.Bundle.Monadic as MB
import           Data.Vector.Fusion.Util
import qualified Data.Vector.Generic               as G
import qualified Data.Vector.Generic.Mutable       as GM
import qualified Data.Vector.Mutable               as VM
import qualified Data.Vector.Primitive             as P
import qualified Data.Vector.Primitive.Mutable     as PM
import qualified Data.Vector.Unboxed               as U
import qualified Data.Vector.Unboxed.Mutable       as UM
import           Debug.Trace
import           Foreign                           hiding (void)
import           GHC.Exts
import           GHC.TypeLits
import           System.IO
import           Unsafe.Coerce

#define MOD 1000000007

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine
    cs <- U.unfoldrN (n*m) (runParser char) . C.filter (not.isSpace) <$> C.getContents
    case solve n m cs of
        (mf, mat) -> do
            print mf
            forM_ [0..n-1] $ \i -> do
                putStrLn . U.toList $ U.slice (i * m) m mat

solve :: Int -> Int -> U.Vector Char -> (Int, U.Vector Char)
solve h w mat = second answer $ bipartiteMatching (h * w) $ \builder -> do
    rep h $ \i -> do
        rep w $ \j -> when (mat U.! ix i j == '.') $ do
            neighbor4 i j $ \ni nj -> when (inGrid h w ni nj && mat U.! ix ni nj == '.') $ do
                addEdgeBMB (ix i j) (ix ni nj) builder
    return ()
  where
    ix i j = i * w + j
    {-# INLINE ix #-}

    answer :: U.Vector Int -> U.Vector Char
    answer m = flip U.imap m $ \xy xy' ->
        if xy' == nothingBM
        then mat U.! xy
        else let (x, y) = quotRem xy w
                 (x', y') = quotRem xy' w
             in case (x' - x, y' - y) of
                    (1, 0) -> 'v'
                    (-1,0) -> '^'
                    (0, 1) -> '>'
                    (0, -1) -> '<'

inGrid :: Int -> Int -> Int -> Int -> Bool
inGrid h w x y = 0 <= x && x < h && 0 <= y && y < w
{-# INLINE inGrid #-}

neighbor4 :: (Applicative f) => Int -> Int -> (Int -> Int -> f ()) -> f ()
neighbor4 x y f = f (x - 1) y *> f x (y - 1) *> f x (y + 1) *> f (x + 1) y
{-# INLINE neighbor4 #-}


type Vertex = Int

bipartiteMatching
    :: Int -- ^ number of vertices
    -> (forall s . BipartiteMatchingBuilder s -> ST s ()) -> (Int, U.Vector Vertex)
bipartiteMatching n run = runST $ do
    builder <- newBipartiteMatchingBuilder n
    run builder
    bm <- buildBipartiteMatching builder
    m <- runBipartiteMatching bm
    (,) m <$> U.freeze (matchBM bm)

data BipartiteMatching s = BipartiteMatching
    { numVerticesBM :: !Int
    , matchBM       :: !(UM.MVector s Int)
    , usedBM        :: !(UM.MVector s Bool)
    , offsetBM      :: !(U.Vector Int)
    , adjacentBM    :: !(U.Vector Int)
    }

nothingBM :: Int
nothingBM = -1
{-# INLINE nothingBM #-}

dfsBM :: (PrimMonad m) => BipartiteMatching (PrimState m) -> Vertex -> (Bool -> m ()) -> m ()
dfsBM BipartiteMatching{..} = dfs
  where
    dfs !v k = UM.unsafeRead usedBM v >>= \case
        True -> k False
        False -> do
            UM.unsafeWrite usedBM v True
            let begin = U.unsafeIndex offsetBM v
            let end = U.unsafeIndex offsetBM (v + 1)
            flip fix begin $ \loop !i -> do
                if i < end
                then do
                    let nv = U.unsafeIndex adjacentBM i
                    mnv <- UM.unsafeRead matchBM nv
                    if mnv == nothingBM
                    then do
                        UM.unsafeWrite matchBM v nv
                        UM.unsafeWrite matchBM nv v
                        k True
                    else do
                        dfs mnv $ \case
                            True -> do
                                UM.unsafeWrite matchBM v nv
                                UM.unsafeWrite matchBM nv v
                                k True
                            False -> loop (i + 1)
                else k False
{-# INLINE dfsBM #-}

runBipartiteMatching :: (PrimMonad m)
    => BipartiteMatching (PrimState m) -> m Int
runBipartiteMatching bm@BipartiteMatching{..} = do
    res <- UM.replicate 1 0
    updated <- UM.replicate 1 True
    fix $ \loop -> do
        UM.unsafeWrite updated 0 False
        rep numVerticesBM $ \i -> do
            mi <- UM.unsafeRead matchBM i
            when (mi == nothingBM) $ do
                dfsBM bm i $ \case
                    True -> do
                        UM.unsafeWrite updated 0 True
                        UM.unsafeModify res (+1) 0
                    False -> return ()
        UM.unsafeRead updated 0 >>= \case
            True -> do
                UM.set usedBM False
                loop
            False -> UM.unsafeRead res 0
{-# INLINE runBipartiteMatching #-}

data BipartiteMatchingBuilder s = BipartiteMatchingBuilder
    { numVerticesBMB :: !Int
    , inDegreeBMB    :: UM.MVector s Int
    , edgesBMB       :: VecStack s (Vertex, Vertex)
    }

newBipartiteMatchingBuilder :: (PrimMonad m)
    => Int -> m (BipartiteMatchingBuilder (PrimState m))
newBipartiteMatchingBuilder n = BipartiteMatchingBuilder n
    <$> UM.replicate n 0
    <*> newVecStack (1024 * 1024)

addEdgeBMB :: (PrimMonad m)
    => Vertex -> Vertex -> BipartiteMatchingBuilder (PrimState m) -> m ()
addEdgeBMB !src !dst BipartiteMatchingBuilder{..} = do
    UM.unsafeModify inDegreeBMB (+1) src
    pushVS (src, dst) edgesBMB
{-# INLINE addEdgeBMB #-}

buildBipartiteMatching :: (PrimMonad m)
    => BipartiteMatchingBuilder (PrimState m)
    ->  m (BipartiteMatching (PrimState m))
buildBipartiteMatching BipartiteMatchingBuilder{..} = do
    let numVerticesBM = numVerticesBMB
    matchBM <- UM.replicate numVerticesBM nothingBM
    usedBM <- UM.replicate numVerticesBM False
    offsetBM <- U.scanl' (+) 0 <$!> U.unsafeFreeze inDegreeBMB
    madjacentBM <- UM.unsafeNew (U.last offsetBM)
    moffset <- U.thaw offsetBM
    edges <- freezeVecStack edgesBMB
    U.forM_ edges $ \(src, dst) -> do
        offset <- UM.unsafeRead moffset src
        UM.unsafeWrite moffset src (offset + 1)
        UM.unsafeWrite madjacentBM offset dst
    adjacentBM <- U.unsafeFreeze madjacentBM
    return BipartiteMatching{..}
{-# INLINE buildBipartiteMatching #-}

-------------------------------------------------------------------------------
-- Utils
-------------------------------------------------------------------------------
rep :: (Monad m) => Int -> (Int -> m ()) -> m ()
rep n = flip MS.mapM_ (stream 0 n)
{-# INLINE rep #-}
rev :: (Monad m) => Int -> (Int -> m ()) -> m ()
rev !n = flip MS.mapM_ (streamR 0 n)
{-# INLINE rev #-}
stream :: (Monad m) => Int -> Int -> MS.Stream m Int
stream !l !r = MS.Stream step l where { step x | x < r = return $ MS.Yield x (x + 1) | otherwise = return MS.Done; {-# INLINE [0] step #-}}
{-# INLINE [1] stream #-}
streamR :: (Monad m) => Int -> Int -> MS.Stream m Int
streamR !l !r = MS.Stream step (r - 1) where { step x | x >= l = return $ MS.Yield x (x - 1) | otherwise = return MS.Done; {-# INLINE [0] step #-}}
{-# INLINE [1] streamR #-}
stream' :: (Monad m) => Int -> Int -> Int -> MS.Stream m Int
stream' !l !r !d = MS.Stream step l where { step x | x < r = return $ MS.Yield x (x + d) | otherwise = return MS.Done; {-# INLINE [0] step #-}}
{-# INLINE [1] stream' #-}
infixl 8 `shiftRL`, `unsafeShiftRL`
shiftRL :: Int -> Int -> Int
shiftRL = unsafeShiftRL
{-# INLINE shiftRL #-}
unsafeShiftRL :: Int -> Int -> Int
unsafeShiftRL (I# x#) (I# i#) = I# (uncheckedIShiftRL# x# i#)
{-# INLINE unsafeShiftRL #-}
type Parser a = StateT C.ByteString Maybe a
runParser :: Parser a -> C.ByteString -> Maybe (a, C.ByteString)
runParser = runStateT
{-# INLINE runParser #-}
int :: Parser Int
int = coerce $ C.readInt . C.dropWhile isSpace
{-# INLINE int #-}
int1 :: Parser Int
int1 = fmap (subtract 1) int
{-# INLINE int1 #-}
char :: Parser Char
char = coerce C.uncons
{-# INLINE char #-}
byte :: Parser Word8
byte = coerce B.uncons
{-# INLINE byte #-}
skipSpaces :: Parser ()
skipSpaces = modify' (C.dropWhile isSpace)
{-# INLINE skipSpaces #-}
lowerBoundM :: (Monad m) => Int -> Int -> (Int -> m Bool) -> m Int
lowerBoundM low high p = go low high where { go !low !high | high <= low = return high | otherwise = p mid >>= bool (go (mid + 1) high) (go low mid) where { mid = low + unsafeShiftRL (high - low) 1}}
{-# INLINE lowerBoundM #-}
upperBoundM :: (Monad m) => Int -> Int -> (Int -> m Bool) -> m Int
upperBoundM low high p = do { flg <- p high; if flg then return high else subtract 1 <$!> lowerBoundM low high (fmap not . p)}
{-# INLINE upperBoundM #-}
lowerBound :: Int -> Int -> (Int -> Bool) -> Int
lowerBound low high p = runIdentity (lowerBoundM low high (return . p))
{-# INLINE lowerBound #-}
upperBound :: Int -> Int -> (Int -> Bool) -> Int
upperBound low high p = runIdentity (upperBoundM low high (return . p))
{-# INLINE upperBound #-}
-------------------------------------------------------------------------------
-- Data.VecStack
-------------------------------------------------------------------------------
data VecStack s a = VecStack{intVarsVS :: !(UM.MVector s Int), internalVecStack :: !(UM.MVector s a)}
_sizeVS :: Int
_sizeVS = 0
{-# INLINE _sizeVS #-}
newVecStack :: (PrimMonad m, UM.Unbox a) => Int -> m (VecStack (PrimState m) a)
newVecStack n = VecStack <$> UM.replicate 1 0 <*> UM.unsafeNew n
defaultVecStackSize :: Int
defaultVecStackSize = 1024 * 1024
popVS :: (PrimMonad m, UM.Unbox a) => VecStack (PrimState m) a -> m (Maybe a)
popVS (VecStack info s) = do { len <- UM.unsafeRead info _sizeVS; if len > 0 then do { UM.unsafeWrite info _sizeVS (len - 1); pure <$> UM.unsafeRead s (len - 1)} else return Nothing}
{-# INLINE popVS #-}
pushVS :: (PrimMonad m, UM.Unbox a) => a -> VecStack (PrimState m) a -> m ()
pushVS x (VecStack info s) = do { len <- UM.unsafeRead info _sizeVS; UM.unsafeWrite s len x; UM.unsafeWrite info _sizeVS (len + 1)}
{-# INLINE pushVS #-}
pushesVS :: (PrimMonad m, UM.Unbox a) => U.Vector a -> VecStack (PrimState m) a -> m ()
pushesVS vec (VecStack info s) = do { len <- UM.unsafeRead info _sizeVS; UM.unsafeWrite info _sizeVS (len + U.length vec); U.unsafeCopy (UM.unsafeSlice len (U.length vec) s) vec}
{-# INLINE pushesVS #-}
freezeVecStack :: (PrimMonad m, U.Unbox a) => VecStack (PrimState m) a -> m (U.Vector a)
freezeVecStack (VecStack info s) = do { l <- UM.unsafeRead info _sizeVS; U.unsafeFreeze $ UM.take l s}
{-# INLINE freezeVecStack #-}