This page is still under construction.

Parts of this page are still being built. What you see may change.

Magical Maze

Time limit1.5sMemory limit512 MB

Summary
Count ordered pairs of chambers (x, y) that both lie on some path from the entrance to the exit of a one-way grid maze, where y is reachable from x.
Level

Hard8 of 10

Topics
Graph, Dynamic programming, Topological sort
Solved
No attempts yet

Problem

Maggy's class went on an excursion to a maze. The maze is a rectangle nn meters high and mm meters wide, made of n×mn \times m square chambers of size 1×11 \times 1 meter each. Between any two chambers that share a side there is a one-directional passage. Some passages are closed for repairs. It is not even known whether the exit can be reached from the entrance.

Before entering, Maggy gets a map that shows the direction of each passage and which passages are closed. The entrance is the upper left chamber, and the only exit is the bottom right chamber. The map also guarantees that you cannot loop forever in the maze: if you leave any chamber through any passage, you cannot get back to that chamber.

Maggy wants to start at the entrance, go through the maze, and leave through the exit. She also writes down the numbers of two favourite chambers she visited, in the order she visited them. One chamber may be written twice. If Maggy fails to leave the maze, she is upset and writes nothing. Given the map, count the number of different ways Maggy can write down the two numbers.

Input

The first line contains two integers nn and mm separated by a single space (1≤n×m≤500 0001 \leq n \times m \leq 500\,000). The following 2n−12n-1 lines contain the map of the maze.

The (2i)(2i)-th line (1≤i≤n1 \leq i \leq n) is a string of m−1m-1 characters from the set {>, <, *}, describing the passages between consecutive chambers in the ii-th row. If the jj-th character is >, there is a passage from the jj-th to the (j+1)(j+1)-st chamber in that row. < means there is a passage from the (j+1)(j+1)-st to the jj-th chamber. * means there is no passage between them in either direction.

The (2i+1)(2i+1)-th line (1≤i≤n−11 \leq i \leq n-1) is a string of mm characters from the set {v, ^, *}, describing the passages between the ii-th and (i+1)(i+1)-st rows. If the jj-th character is v, there is a passage from the jj-th chamber in the ii-th row to the jj-th chamber in the (i+1)(i+1)-st row. ^ means the passage goes the other way. * means there is no passage in either direction.

The entrance is the first chamber in the first row, and the exit is the last chamber in the last row.

Output

Print one integer: the number of ways Maggy can write down the numbers of two visited chambers, in the order of visiting them. If the exit cannot be reached, print 0.

Hint

There is only one way to reach the exit: first go right twice, then go down once.

Examples1

  1. Example 1

    Input
    2 3
    >>
    *^v
    <>
    
    Expected output
    10