BFS to check connectivity