python solution sort + bfs O(n ** 2)
```\nclass Solution(object):\n    def cutOffTree(self, forest):\n        row, col = len(forest), len(forest[0]) \n        def bfs(i,j, target):\n            que
```\nclass Solution(object):\n    def cutOffTree(self, forest):\n        row, col = len(forest), len(forest[0]) \n        def bfs(i,j, target):\n            queue = [(i,j, 0)] \n            move = 0 \n            s = {(i,j)}\n            while queue:\n                i,j, move = queue.pop(0) \n                if forest[i][j] == target:\n                    return move \n                for x,y in (i+1,j),(i,j+1),(i,j-1),(i-1,j):\n                    if 0 <= x < row and 0 <= y < col and (x,y) not in s:\n                        if forest[x][y]:\n                           queue.append((x,y,move+1)) \n                           s.add((x,y))\n            return -1 \n        g = sorted((forest[i][j],i,j) for i in range(row) for j in range(col) if forest[i][j] > 1)\n        ans = 0\n        x = y = 0\n        while g:\n            h,i,j = g.pop(0)\n            move = bfs(x,y, h) \n            if move == -1:\n                return -1 \n            ans += move \n            x,y = i,j \n        return ans\n
[]