You and Prem are playing a game show called blind maze.
Prem will be at inside the grid and will be blind folded, and you will be guiding him to victory.
In this game you are given a N x N maze A. The maze has some pillars within the grid, and the rest are on the outskirts of the grid, such that Prem can never leave the grid.
Prem starts off from (1, 1) and can never step on a pillar.
You can only give the commands, move forward, turn 90° left , turn 90° right or do nothing.
But as the prize money is huge, there has to be some twist. In this game, Prem can start facing up or right, and that direction is not known to you.
If you give an instruction such that Prem is going to collide with a pillar, then Prem will not move at all.
Find the minimum length of instructions such that no matter which side Prem is facing, he will be able to exit the grid i.e. reach N x N.
If he is not able to exit then return -1.
Input Format:
The first and the only argument of input contains N strings of length N, representing the grid.
=> Grid[i][j] = 0 : No Pillar.
=> Grid[i][j] = 1 : Pillar.
Output Format:
Return an integer representing the minimum possible length of instructions.
Constraints:
1 <= N <= 100
How to Approach this Problem?