java accepted BFS solution
This Problem is similiar to  walls and land question on leetcode. Just apply the BFS . we are given a starting point . So apply BFS from that only on empty cell
This Problem is similiar to  **walls and land** question on leetcode. Just apply the BFS . we are given a starting point . So apply BFS from that only on empty cells  and whenever we are at 1st row, or 1st column or last row or last column simply return that level. \n\'\'\'\nclass Solution {\n    public int nearestExit(char[][] grid, int[] entrance) {\n        if (grid.length == 0 || grid[0].length == 0)\n            return-1;\n       \n\n        int n = grid.length, m = grid[0].length;\n        LinkedList<Integer> que = new LinkedList<>();\n        int[][] dir = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };\n\n        int startrow =entrance[0];\n        int startcol = entrance[1];\n         que.addLast(startrow * m + startcol);\n        grid[startrow][startcol] = \'+\';\n        int level=0;\n        while (que.size() != 0) {\n            int size = que.size();\n            level++;\n            while (size-- > 0) {\n                \n                int idx = que.removeFirst();\n                int sr = idx / m, sc = idx % m;\n\n                for (int d = 0; d < dir.length; d++) {\n                    int r = sr + dir[d][0];\n                    int c = sc + dir[d][1];\n\n                    if (r >= 0 && c >= 0 && r < n && c < m && grid[r][c] == \'.\') {\n                        if(r==0 || r== n-1 || c==0 ||c==m-1) return level;\n                        grid[r][c]=\'+\';\n                        que.addLast(r * m + c);\n                    }\n                }\n            }\n        \n        \n    }\n        return -1;\n    }\n}\n\'\'\'
['Breadth-First Search', 'Java']