
This problem can be simplified to an n shortest distance problem.
We start from arr[0][0], looking for the next larger element .... until we reach the last one. This requires sorting and queue and traverse all element, we can take use of priority queue.
1. put all elements into a priority queue q, put smaller into the front.
2.from arr[0][0], to q.poll(), we use bfs to calculate the shortest distance.
3. until we reach the last element, return the total amount of all distances.
bfs:
1. Initialize a seen array which fills in false.
2. add an element in to queue,.
3. poll an element interatively, if it's the aim value, return the value's path length.
4. move the element in array 1 unit to get a new element. Eachtime we check the boundaries, whether it's a wall and whether it has been reached before.
4. If pass all conditions, add the new element into the queue
5. set the seen arr of the new element to true, representing checked
6. until there's no new element, return -1.
Code:

