Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Friday, Dec 4.Claim the deal

(This question is a variation of the LeetCode question 1091. Shortest Path in Binary Matrix. If you haven't completed that question yet, it is recommended to solve it first.)

You are given an m×nm × n grid where each cell contains a digit, find the shortest path from the top-left corner (0,0)(0, 0) to the bottom-right corner (m−1,n−1)(m - 1, n - 1). A cell with a value from 1 to 9 is walkable, while a cell with a value of 0 represents a wall that cannot be traversed. You can move in all eight possible directions (up, down, left, right, and the four diagonals).

Test Cases
Input
Output
Expected