Number of Squares and Rectangles in a Grid

I was asked how many squares and rectangles can you find in a 4 x 3 grid? (The squares and rectangles can be constructed from multiple unit squares. For example, there are 8 squares and 18 rectangles in a 2 x 3 grid and 20 squares and 60 rectangles in a 3 x 4 grid.)

Does the general problem of finding the number of squares and rectangles in a m x n grid pertain to any branch of mathematics? If so, what branch? A formula to determine the answer to the general question would be appreciated. A proof would be even more appreciated.