There is a general formula for finding out the number of squares in a square = $\frac{n(n+1)(2n+1)}{6}$ where n represents the side of a $n*n$ square.
Derivation of the formula :
Square of length 1 is 16 (i.e. $4^2$ )
Square of length 2 is 9 (i.e. $3^2$)
Square of length 3 is 4 (i.e. $2^2$)
Square of length 4 is 1 (i.e.$1^2$).
So in general number of squares in a square of side n is $1^2$ + $2^2$ + $3^2$ +...... $n^2$ = $\frac{n(n+1)(2n+1)}{6}$
Hence by the formula we have $\frac{4.5.9}{6}$ = 30