Number of squares in a rectangle Given a rectangle of length a and width b (as shown in the figure), how many different squares of edge greater than 1 can be formed using the cells inside.

For example, if a = 2, b = 2, then the number of such squares is just 1.

Without loss of generality we may assume that that the width $b$ is $\ge$ to the height $a$. Note that there are $b+1$ vertical lines and $a+1$ horizontal lines in the picture. 
If $a\ge 2$, the number of $2\times 2$ squares is $(a-1)(b-1)$. This is because the top left corner of a $2\times 2$ square can be any corner that is not in the last two columns or the last two rows. 
If $a\ge 3$, the number of $3\times 3$ squares is $(a-2)(b-2)$. This is because the top left corner of a $3\times 3$ square can be any corner which is not in the last three columns or the last three rows. 
If $a\ge 4$, the number of $4\times 4$ squares is $(a-3)(b-3)$.
Continue. Finally, the number of $a\times a$ squares is (a-(a-1))(b-(a-1))$.
Add up.  To get a "nice" expression for the sum, note that
$$(a-k)(b-k)=ab-k(a+b)+k^2.\tag{$1$}$$
When we add up the expressions in $(1)$, we get $a-1$ terms with value $ab$, for a total of $(a-1)ab$.
The "middle" terms add up to $-(a+b)(1+2+\cdots+(a-1))$. The sum is 
$-(a+b)\dfrac{(a-1)a}{2}$.
Finally, we need $1^2+2^2+\cdots +(a-1)^2$. By the usual formula for the sum of consecutive squares, this is equal to $\dfrac{(a-1)(a)(2a-1)}{6}$.
