99.45 in speed
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def cutOffTree(self, forest: List[List[int]]) -> int:\n        if forest[0][0] == 0 :\n            return -1\n        m = len(forest)\n        n = len(forest[0])\n        def distance(node1, node2):\n            now = [node1]\n            soon = []\n            expanded = set()\n            manhattan = abs(node1[0] - node2[0]) + abs(node1[1] - node2[1])\n            detours = 0\n            while True:\n                if len(now) == 0:\n                    now = soon\n                    soon = []\n                    detours += 1\n                node = now.pop()\n                if node == node2:\n                    return manhattan + 2 * detours\n                if node not in expanded:\n                    expanded.add(node)\n                    x, y = node\n                    if x - 1 >= 0 and forest[x - 1][y] >= 1:\n                        if x > node2[0]:\n                            now.append((x - 1, y))\n                        else:\n                            soon.append((x - 1, y))\n                    if y + 1 < n and forest[x][y + 1] >= 1:\n                        if y < node2[1]:\n                            now.append((x, y + 1))\n                        else:\n                            soon.append((x, y + 1))\n                    if x + 1 < m and forest[x + 1][y] >= 1:\n                        if x < node2[0]:\n                            now.append((x + 1, y))\n                        else:\n                            soon.append((x + 1, y))\n                    if y - 1 >= 0 and forest[x][y - 1] >= 1:\n                        if y > node2[1]:\n                            now.append((x, y - 1))\n                        else:\n                            soon.append((x, y - 1))\n        trees = []\n        for i in range(m):\n            for j in range(n):\n                if forest[i][j] > 1:\n                    trees.append(((i, j), forest[i][j]))\n        trees.sort(key=lambda x: x[1])\n        can_reach = {(0, 0)}\n        stack = [(0, 0)]\n        while len(stack) > 0:\n            x, y = stack.pop()\n            if x - 1 >= 0 and forest[x - 1][y] >= 1 and (x - 1, y) not in can_reach:\n                can_reach.add((x - 1, y))\n                stack.append((x - 1, y))\n            if y + 1 < n and forest[x][y + 1] >= 1 and (x, y + 1) not in can_reach:\n                can_reach.add((x, y + 1))\n                stack.append((x, y + 1))\n            if x + 1 < m and forest[x + 1][y] >= 1 and (x + 1, y) not in can_reach:\n                can_reach.add((x + 1, y))\n                stack.append((x + 1, y))\n            if y - 1 >= 0  and forest[x][y - 1] >= 1 and (x, y - 1) not in can_reach:\n                can_reach.add((x, y - 1))\n                stack.append((x, y - 1))\n        for t in trees:\n            if t[0] not in can_reach:\n                return -1\n        start = (0, 0)\n        num_step = 0\n        for t in trees: \n            num_step += distance(start, t[0])\n            forest[t[0][0]][t[0][1]] = 1\n            start = t[0]\n        return num_step\n```
['Python3']