Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1. A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that all the visited cells of the path are 0, and all the adjacent cells of the path are 8-directionally connected. The length of a clear path is the number of visited cells of this path.
Example 1
Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Explanation: Go right from (0,0) to (0,1), then diagonally to (1,2), then down to (2,2). The path visits 4 clear cells.
Example 2
Input: grid = [[0,1],[1,0]]
Output: 2
Explanation: Move diagonally from (0,0) straight to (1,1). The path visits 2 cells.
Example 3
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
Output: -1
Explanation: The starting cell (0,0) is blocked (1), so no clear path can begin. The answer is -1.
BFS explores cells in order of distance, so the first time it reaches the bottom-right corner gives the shortest path length. Moves go to all 8 neighbours.
Run checks all cases above. Submit evaluates all test cases.