How many ways are there to write all numbers from 1 to 9 in the cells of a $3 \times 3$ grid so that for all integers $1 \leq n<9$, the cell labeled $n$ shares an edge with the cell labeled $n+1$ ?
Apply a checkerboard coloring to the grid so that the corners and the center are black. Then by parity, the 5 odd numbers must be in the 5 black cells and the 4 even numbers in the 4 white cells.

If the center cell is " 1 ", there are 4 ways to pick " 2 " and 2 ways to pick " 3 ", at which point the rest of the numbers are determined, for a total of 8 possibilities. By symmetry there are 8 possibilities for the center cell to be 9 . Likewise, if the center cell is " 3 ", picking " 2 " and " 1 " gives 8 more, and another 8 more for the center cell being " 7 ". Finally, if the center cell is " 5 ", the path is still determined by picking " 6 " and " 7 ". In total, there are therefore $5 \cdot 8=40$ possibilities.