I have a closed figure of pixels. Pixels with 3 or more neighbors I highlighted in red.
In the worst case, the workload is proportional to the sum of the lengths of the paths between the starting point and the related red points, and this is optimal.
In the best case, it is the length of the shortest path between the two given points.