In a sliding game a counter may slide horizontally or vertically into an empty space. The objective of the game is to move the red counter from the top left corner of a grid to the bottom right corner; the space always starts in the bottom right corner. For example, the following sequence of pictures show how the game can be completed in five moves on a 2 by 2 grid.
<imgclass="img-responsive center-block"alt="completing game in five moves on grid 2x2"src="https://cdn.freecodecamp.org/curriculum/project-euler/sliding-game-1.gif"style="background-color: white; padding: 10px;">
<imgclass="img-responsive center-block"alt="initial grid state and final grid state for game on grid 5x4"src="https://cdn.freecodecamp.org/curriculum/project-euler/sliding-game-2.gif"style="background-color: white; padding: 10px;">
There are exactly 5482 grids for which $S(m, n) = p^2$, where $p < 100$ is prime.
How many grids does $S(m, n) = p^2$, where $p < {10}^6$ is prime?