Simple Python BFS, No TLE
I modified the solution to pass in length or rows and columns and did visited True before adding to queue, that got rid of TLE.\nRuntime:  6252 ms\tMemory: 14.2
I modified the solution to pass in length or rows and columns and did visited True before adding to queue, that got rid of TLE.\nRuntime:  6252 ms\tMemory: 14.2 MB Rank: 58%\n```\nclass Solution:\n    def cutOffTree(self, forest: List[List[int]]) -> int:\n        \n        m, n = len(forest), len(forest[0])\n        trees = []\n        \n        for r in range(m):\n            for c in range(n):\n                v = forest[r][c]\n                if v > 1:\n                    trees.append((v, r, c)) \n                \n        trees.sort()\n        \n        sr, sc = 0, 0\n        total_cost = 0\n        for tree in trees:\n            height, tr, tc = tree\n            d = self.bfs(forest, sr, sc, tr, tc, m, n)\n            if d < 0: return -1\n            total_cost += d\n            sr, sc = tr, tc\n            \n        return total_cost\n    \n    def bfs(self, forest, sx, sy, tx, ty, m, n):\n        \n        queue = collections.deque([(sx, sy, 0)])\n        visited = [[False]*n for _ in range(m)]\n        visited[sx][sy] = True\n        \n        while queue:\n            sr, sc, d = queue.popleft()\n            if sr == tx and sc == ty: return d\n            for x, y in [[1, 0], [0, 1], [-1, 0], [0, -1]]:\n                nr, nc = sr+x, sc+y\n                if 0 <= nr < m and 0 <= nc < n and not visited[nr][nc] and forest[nr][nc]:\n                    visited[nr][nc] = True\n                    queue.append((nr, nc, d+1))\n        \n        return -1\n\t\t
[]