fixed codeforces round 222 problem C

was giving a TLE because I was calling BFS n times (because the break
statement will only break out of the inner loop)