Do a BackFlip for ais523! This challenge is a prize for ais523 for winning the "Rookie of the Year" category in "Best of PPCG 2016". Congratulations!



BackFlip is an esoteric programming language made by user ais523, who has created well over 30 other interesting esolangs.

BackFlip is a 2D language like Befunge or >&lt;> where the instruction pointer traverses a grid of text (the program), moving up, down, left, and right, changing direction depending on the character it's on. Critically, the grid in a BackFlip program changes as it is being traversed, a bit like Langton's Ant.

For this challenge you may assume that a BackFlip program is always a rectangular grid of text (all lines the same length), 1&times;1 in size at minimum, only containing the characters ./\&lt;&gt;^V. (. is used for visibility rather than space.) Semantically the BackFlip we'll use here is identical to the original spec.

The instruction pointer (IP) in BackFlip always starts just left of the top-left corner of the program, heading right. There are three types of commands it can encounter:


. is a no-op. The IP continues on in the direction it was going. The no-op stays a no-op.
/ and \ are mirrors. They reflect the IP in the direction indicated by their angle, then they change into the other type of mirror.


For example, if the IP heads left into a \, it starts moving upward instead of left and the \ becomes a /.

&lt;, &gt;, ^, and V are arrows. They redirect the IP to the direction they point in, then they change into an arrow that points in the direction the IP came from (opposite the direction the IP was moving).


For example, if the IP heads downward into &gt;, it starts moving right instead of downwards and the &gt; becomes a ^ because that is the direction the IP came from.



A BackFlip program terminates when the IP moves out of bounds, i.e. goes off the grid. It turns out all BackFlip programs eventually end because infinite loops are impossible. (You may assume this is true.)

Your goal in this challenge is to write a program or function that takes in a BackFlip program and outputs the number of moves the instruction pointer takes before the program terminates. That is, how many steps does the IP take in the course of running a program? This includes the initial step onto the grid and the final step off of it.

For example, the instruction pointer takes 5 steps in the trivial grid ....:

 ....  &lt;- empty 4×1 grid
012345 &lt;- step number of the IP


So the output to .... is 5.

In the more complex 4&times;2 grid

\...
\.&gt;&lt;


the IP exits the grid on its 9th step, so the output is 9:

step  grid  IP position (@)
0     \...  @....
      \.&gt;&lt;   ....

1     \...   @...
      \.&gt;&lt;   ....

2     /...   ....
      \.&gt;&lt;   @...

3     /...   ....
      /.&gt;&lt;   .@..

4     /...   ....
      /.&gt;&lt;   ..@.

5     /...   ....
      /.&lt;&lt;   ...@

6     /...   ....
      /.&lt;&lt;   ..@.

7     /...   ....
      /.&gt;&lt;   .@..

8     /...   ....
      /.&gt;&lt;   @...

9     /...   ....
      \.&gt;&lt;   ....
             @


The shortest code in bytes wins.

You may take input as an array of lines or matrix of characters instead of a multiline string if desired, but you must use the characters ./\&lt;&gt;^V (not integer opcodes). You may use space instead of . if preferred. It's fine if characters like \ need to be escaped in input. Output is always an integer more than one.

Test Cases

....
5

\...
\.&gt;&lt;
9

.
2

..
3

.
.
2

\
2

^
2

.^.
3

&lt;.
2

\\
\/
7

&gt;V
^&lt;
6

&gt;\
&gt;/
6

\&gt;&lt;
2

\&gt;&lt;
\&gt;&lt;
7

\&gt;&lt;
\&gt;&lt;
\&gt;&lt;
12

\.V.
\.\&lt;
5

\.V.
\./&lt;
9

V./\
V./\
&gt;./&lt;
..\/
14

\V..
.^..
\/&gt;&lt;
.V..
.^..
20

\.V.V.
\./.\&lt;
.&gt;\&lt;..
..^.^.
31

\.V.V.V.
\./&gt;/.\&lt;
.&gt;\&gt;\&lt;..
..^.^.^.
69

\.V.V.V.V.
\./&gt;/&gt;/.\&lt;
.&gt;\&gt;\&gt;\&lt;..
..^.^.^.^.
145

\.V.V.V.V.V.V.V.V.V.V.
\./&gt;/&gt;/&gt;/&gt;/&gt;/&gt;/&gt;/&gt;/.\&lt;
.&gt;\&gt;\&gt;\&gt;\&gt;\&gt;\&gt;\&gt;\&gt;\&lt;..
..^.^.^.^.^.^.^.^.^.^.
9721
JavaScript (ES6), 158 bytes

f=(a,x=0,y=0,d=3)=&gt;a[x]&amp;&amp;(c=a[x][y])?(a[x][y]=c=='.'?c:c=='/'?(d^=3,'\\'):c=='\\'?(d^=1,'/'):'v&gt;^&lt;'[d][d='^&lt;v&gt;'.search(c),0],f(a,d&lt;3?x+d-1:x,d?y+d-2:y,d)+1):1


Developed independently from @tsh's answer although strikingly similar.

The mapping of directions ^&lt;v&gt; to integers 0-3 is governed by the fact that .search('^') returns 0 since ^ is a regexp metacharacter.