ByteDance Interview Question

BFS problem.