Back to the Problem
BFS from S computes shortest unweighted distances layer by layer.
Complexity: O(N + M)