A board has N rows and M columns. Each cell contains either a digit from 1 to 9 or H, which represents a hole. A coin starts on the top-left cell, and that cell is never a hole.
On each move, look at the digit X written on the coin's current cell. Choose one of the four directions: up, down, left, or right. Then move the coin exactly X cells in that direction. Holes passed over during the move are ignored.
If the coin lands in a hole or moves outside the board, the game ends. Find the maximum number of moves that can be made. If it is possible to keep moving forever, print -1.