Problem:
Magic Tile

There is a magic room in a homestead. The room is paved with H × W tiles. There are five different tiles:

Tile with a east-pointing arrow

Tile with a west-pointing arrow

Tile with a south-pointing arrow

Tile with a north-pointing arrow

Tile with nothing

Once a person steps onto a tile which has an arrow, the mystic force makes the person go to the next tile pointed by the arrow. If the next tile has an arrow, the person moves to the next, ans so on. The person moves on until he/she steps onto a tile which does not have the arrow (the tile with nothing). The entrance of the room is at the northwest corner.

Your task is to write a program which simulates the movement of the person in the room. The program should read strings which represent the room and print the last position of the person.

The input represents the room as seen from directly above, and up, down, left and right side of the input correspond to north, south, west and east side of the room respectively. The horizontal axis represents x-axis (from 0 to W-1, inclusive) and the vertical axis represents y-axis (from 0 to H-1, inclusive). The upper left tile corresponds to (0, 0).

The following figure shows an example of the input:

10 10
>>>v..>>>v
...v..^..v
...>>>^..v
.........v
.v<<<<...v
.v...^...v
.v...^<<<<
.v........
.v...^....
.>>>>^....

Characters represent tiles as follows:

'>': Tile with a east-pointing arrow
'<': Tile with a west-pointing arrow
'^': Tile with a north-pointing arrow
'v': Tile with a south-pointing arrow
'.': Tile with nothing

If the person goes in cycles forever, your program should print "LOOP". You may assume that the person never goes outside of the room.

Input

The input consists of multiple datasets. The input ends with a line which contains two 0. Each dataset consists of:

H W
H lines where each line contains W characters

You can assume that 0 < W, H < 101.

Output

For each dataset, print the coordinate (X, Y) of the person or "LOOP" in a line. X and Y should be separated by a space.

Sample Input

10 10
>>>v..>>>v
...v..^..v
>>>>>>^..v
.........v
.v<<<<...v
.v.v.^...v
.v.v.^<<<<
.v.v.....v
.v...^...v
.>>>>^....
6 10
>>>>>>>>>v
.........v
.........v
>>>>v....v
^...v....v
^<<<<<<<<<
0 0

Output for the Sample Input

5 7
LOOP

Solution in Ruby (judge verdict: Accepted)


class Coord
  attr_accessor :x, :y
  def initialize(__x, __y)
    @x = __x
    @y = __y
  end
  
  def +(coord)
    Coord.new(@x + coord.x, @y + coord.y)
  end
  
  def zero?
    @x == 0 && @y == 0
  end
  
  def to_s
    "#{@x} #{@y}"
  end
end

class MagicalTile
  attr_accessor :moved
  
  def initialize(tile_symbol)
    @vector = Coord.new(0, 0)
    @moved = false
    case tile_symbol
    when "<"
      @vector.x = -1
    when ">"
      @vector.x = 1
    when "^"
      @vector.y = -1
    when "v"
      @vector.y = 1
    end
  end
  
  def move(current_pos)
    unless @moved
      @moved = true
      current_pos + @vector
    end
  end
  
  def stop?
    @vector.zero?
  end
end

class MagicalFloor
  def initialize(__width, __height)
    @width = __width
    @height = __height
    @tiles = Array.new(@height) do
      input_line(gets.strip)
    end
  end
  
  def input_line(line)
    line.each_char.map{ |c| MagicalTile.new(c) }
  end
  
  def run
    pos = Coord.new(0, 0)
    until tile(pos).stop?
      next_pos = tile(pos).move(pos)
      return "LOOP" unless next_pos
      pos = next_pos
    end
    pos.to_s
  end
  
  def tile(pos)
    @tiles[pos.y][pos.x]
  end
end

def input_floor_size
  gets.strip.split.map(&:to_i)
end

floors = []
while true
  height, width = input_floor_size
  break if width == 0 && height == 0
  floors << MagicalFloor.new(width, height)
end

puts floors.map(&:run).join("\n")