Grouping Objects bfs solution