BFS

www.spoj.com/problems/AMR11J