0-1 BFS | O(n)
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: $$O(n*m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n*m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int dirs[4][2]={{0,1},{0,-1},{1,0},{-1,0}};\n    bool findSafeWalk(vector<vector<int>>& g, int h) {\n        int n=g.size(),m=g[0].size();\n        vector<vector<bool>> vis(n,vector<bool>(m,0));\n        vis[0][0]=1;\n        deque<pair<int,pair<int,int>>> dq;\n        dq.push_back({0,{0,g[0][0]}});\n\n        while(!dq.empty()){\n            pair<int,pair<int,int>> p=dq.front();\n            int a=p.first,b=p.second.first,c=p.second.second;\n            dq.pop_front();\n            if(a==n-1 && b==m-1) return c<h;\n            \n            for(auto dir:dirs){\n                int x=dir[0]+a;\n                int y=dir[1]+b;\n                if(x<0 || y<0 || x>=n || y>=m) continue;\n                int cost;\n                if(!vis[x][y])\n                { if(g[x][y]==0)\n                        dq.push_front({x,{y,c}});\n                    else\n                        dq.push_back({x,{y,c+1}});\n                  vis[x][y]=1;\n                }    \n            }\n        }\n        return 0;\n    }\n};\n```\n```java []\nclass Solution {\n    int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n    \n    public boolean findSafeWalk(List<List<Integer>> grid, int health) {\n        int n = grid.size();\n        int m = grid.get(0).size();\n        boolean[][] vis = new boolean[n][m];\n        vis[0][0] = true;\n        \n        Deque<int[]> dq = new ArrayDeque<>();\n        dq.offer(new int[]{0, 0, grid.get(0).get(0)});\n\n        while (!dq.isEmpty()) {\n            int[] p = dq.poll();\n            int a = p[0], b = p[1], c = p[2];\n            \n            if (a == n - 1 && b == m - 1) return c < health;\n            \n            for (int[] dir : dirs) {\n                int x = a + dir[0];\n                int y = b + dir[1];\n                \n                if (x < 0 || y < 0 || x >= n || y >= m) continue;\n                \n                if (!vis[x][y]) {\n                    if (grid.get(x).get(y) == 0) {\n                        dq.offerFirst(new int[]{x, y, c});\n                    } else {\n                        dq.offerLast(new int[]{x, y, c + 1});\n                    }\n                    vis[x][y] = true;\n                }\n            }\n        }\n        return false;\n    }\n}\n```
['C++']