I am trying to find the number of $M\times M$ sub-grids given an $N\times N$ matrix, where $M \leq N$.
For any concrete example it is easy to find the correct answer: Eg:
$$\left( \begin{array}{ccc} 4 & 3 & 8\\ 9 & 5 & 1\\ 2 & 7 & 6\\ \end{array} \right)$$
This is a $3 \times 3$ matrix: The number of $1 \times 1$ sub-grids is 9; the number of $2 \times 2$ sub-grids is 4; and the number of $3 \times 3$ sub-grids is 1.
I am trying to find a general formula to find the number of sub-grids. So far I have been looking at the dimensions of each:
A $4 \times 4$ matrix will have:
$$16 \quad (1 \times 1)$$ $$9 \quad (2 \times 2)$$ $$4 \quad (3 \times 3)$$ $$1 \quad (4 \times 4)$$
sub-grids.
By trial I have concluded that the number of sub-grids can be calculated by:
$$({N-M+1})^2$$
Could someone please confirm that this is a correct conclusion and maybe give some intuition to why is so? (e.g. the number of shifts it is possible to make with the sub-grids in the matrix)