You are given an m x n integer array grid. Each square holds one of these values:
1is the start. There is exactly one.2is the end. There is exactly one.0is an empty square you can walk over.-1is an obstacle you can't walk over.
Each step moves to the square directly up, down, left or right. Return the number of walks from the start to the end that step on every square that isn't an obstacle exactly once, the start and end included.
Examples
Example 1
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Example 2
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
With the obstacle gone, the end moves to the corner and there are 12 squares to cover. Four different walks cover all of them.
Example 3
Input: grid = [[0,1],[2,0]]
Output: 0
Each 0 is only next to the start and the end. After stepping on one of them, the only way on is the end, so the other 0 is never reached.
Example 4
Input: grid = [[0,1,2]]
Output: 0
The 0 is on the far side of the start. Going there first leaves no way back, and going to the end first skips it.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 201 <= m * n <= 20-1 <= grid[i][j] <= 2- There is exactly one start and exactly one end.