"Turns minimization"
Consider a map of maze in the form of rectangular table filled with zeroes and ones, where "0" means a free cell and "1" means a wall.
Robot initially is in the cell with coordinates (i_0, j_0). It should be moved outside the maze (via any free cell in any outer side). Robot can move to any free of four neighboring cells (left, right, up, down). Robot’s significant feature is that it can pass many free cells in the same direction very quickly, but each turn (direction changing) requires a lot of time.
Your task is to write a program finding the way to move outside the maze with minimal number of turns.
Input
The 1^st line of input contains two integers N, M (5 ≤ N,_{ }M ≤ 640) which are the size of the maze. Each of following N lines contains exactly M ones and/or zeroes (without any delimiters), denoting walls and free cells. The next and the last line contains two integers i_0, j_0 (2 ≤ i_{0 }≤ N–1, 2 ≤ j_{0 }≤ M–1) which are initial coordinates. It’s guaranteed that initial coordinates correspond to free cell, but it’s unknown whether any way out exists.
Output
Output exactly one integer — the minimal number of same-directed fragments of way outside. If no way out exists, program should output "–1" (without quotes).