Breadth-First Search algorithm for N dimensional board.