# Solution sharing

• `````` int m=rooms.length;
if(m==0)
return;
int n=rooms[0].length;
int INF=Integer.MAX_VALUE;
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(rooms[i][j]==0) {
boolean[][] visited=new boolean[m][n];
while(!neighbours.isEmpty()) {

Point next=neighbours.poll();
int r=next.x;int c=next.y;
visited[r][c]=true;
if(r-1>=0  && !visited[r-1][c] && (rooms[r-1][c]==INF || ( rooms[r-1][c]!=-1 && rooms[r-1][c]!=0))) {neighbours.add(new Point(r-1,c));rooms[r-1][c]=Math.min(rooms[r-1][c], rooms[r][c]+1);}
if(r+1<m && !visited[r+1][c] && (rooms[r+1][c]==INF || ( rooms[r+1][c]!=-1 &&  rooms[r+1][c]!=0 ) )) {neighbours.add(new Point(r+1,c));rooms[r+1][c]=Math.min(rooms[r+1][c], rooms[r][c]+1);}
if(c-1>=0 && !visited[r][c-1] && (rooms[r][c-1]==INF || (rooms[r][c-1]!=-1 && rooms[r][c-1]!=0 ))) {neighbours.add(new Point(r,c-1));rooms[r][c-1]=Math.min(rooms[r][c-1], rooms[r][c]+1);}
if(c+1<n && !visited[r][c+1] && (rooms[r][c+1]==INF || (rooms[r][c+1]!=-1 && rooms[r][c+1]!=0 ))) {neighbours.add(new Point(r,c+1));rooms[r][c+1]=Math.min(rooms[r][c+1], rooms[r][c]+1);}
}
}
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.