Finds the shortest path between points in a matrix