class Pair { int first; int second; int step; Pair(int first, int second, int step) { this.first = first; this.second = second; this.step = step; } } class Solution { // similar to Distance of nearest cell having 1 public void islandsAndTreasure(int[][] grid) { ArrayList> res = new ArrayList<>(); int n = grid.length; int m = grid[0].length; Queue q = new LinkedList<>(); // visited array to mark which all land cells are visited boolean[][] visited = new boolean[n][m]; // distance matrix that will store the nearest distance of 0's from land cells int[][] distance = new int[n][m]; // step-1 adding all those indices into queue where we have as 0's and mark them visited for(int i=0; i=0 && nRow < n && nCol >= 0 && nCol < m) { // expand to land cells if(visited[nRow][nCol] == false && grid[nRow][nCol] == 2147483647) { q.add(new Pair(nRow, nCol, step+1)); visited[nRow][nCol] = true; distance[nRow][nCol] = step+1; } } } } for(int i=0; i