Python solution based on wufangjie's (Hadlock's algorithm?)
Just my own very similar implementation of @wufangjie's solution (and some terminology from the Hadlock algorithm which @awice mentioned to me), with some more 
Just my own very similar implementation of @wufangjie's [solution](https://discuss.leetcode.com/topic/103532/my-python-solution-inspired-by-a-algorithm) (and some terminology from the Hadlock algorithm which @awice mentioned to me), with some more explanation. Gets accepted in about 700 ms.\n\nBasically find the trees, sort them by order, find the distance from each tree to the next, and sum those distances. But how to find the distance from one cell to some other cell? BFS is far to slow for the current test suite. Instead use what's apparently known as "Hadlock's Algorithm" (though I've only seen high-level descriptions of that). First try paths with no detour (only try steps in the direction towards the goal), then if necessary try paths with one detour step, then paths with two detour steps, etc. The distance then is the Manhattan distance plus twice the number of detour steps (twice because you'll have to make up for a detour step with a later step back towards the goal).\n\nHow to implement that?\n- Round 1: Run a DFS only on cells that you can reach from the start cell with no detour towards the goal, i.e., only walking in the direction towards the goal. If this reaches the goal, we're done. Otherwise...\n- Round 2: Try again, but this time try starting from all those cells reachable with one detour step. Collect these in round 1.\n- Round 3: If round 2 fails, try again but start from all those cells reachable with two detour steps. Collect these in round 2.\n- And so on...\n\nIf there are no obstacles, then this directly walks a shortest path towards the goal, which is of course very fast. Much better than BFS which would waste time looking in all directions. With only a few obstacles, it's still close to optimal.\n\nMy `distance` function does this searching algorithm. I keep the current to-be-searched cells in my `now` stack. When I move to a neighbor that's closer to the goal, I also put it in `now`. If it's not closer, then that's a detour step so I just remember it on my `soon` stack for the next round.\n\n    def cutOffTree(self, forest):\n\n        # Add sentinels (a border of zeros) so we don't need index-checks later on.\n        forest.append([0] * len(forest[0]))\n        for row in forest:\n            row.append(0)\n\n        # Find the trees.\n        trees = [(height, i, j)\n                 for i, row in enumerate(forest)\n                 for j, height in enumerate(row)\n                 if height > 1]\n\n        # Can we reach every tree? If not, return -1 right away.\n        queue = [(0, 0)]\n        reached = set()\n        for i, j in queue:\n            if (i, j) not in reached and forest[i][j]:\n                reached.add((i, j))\n                queue += (i+1, j), (i-1, j), (i, j+1), (i, j-1)\n        if not all((i, j) in reached for (_, i, j) in trees):\n            return -1\n\n        # Distance from (i, j) to (I, J).\n        def distance(i, j, I, J):\n            now, soon = [(i, j)], []\n            expanded = set()\n            manhattan = abs(i - I) + abs(j - J)\n            detours = 0\n            while True:\n                if not now:\n                    now, soon = soon, []\n                    detours += 1\n                i, j = now.pop()\n                if (i, j) == (I, J):\n                    return manhattan + 2 * detours\n                if (i, j) not in expanded:\n                    expanded.add((i, j))\n                    for i, j, closer in (i+1, j, i < I), (i-1, j, i > I), (i, j+1, j < J), (i, j-1, j > J):\n                        if forest[i][j]:\n                            (now if closer else soon).append((i, j))\n\n        # Sum the distances from one tree to the next (sorted by height).\n        trees.sort()\n        return sum(distance(i, j, I, J) for (_, i, j), (_, I, J) in zip([(0, 0, 0)] + trees, trees))
[]