How many squares, generalized

This is a generalization of the question how many squares. Instead a 5 x 5 grid of dots, consider an n x n grid of dots. How many squares can you make on an n x n grid of dots such that all four corners of the square lie on a dot?

Solution