We can calculate the number of square or rectangle in a $n\cdot n$ grid.
No of squares $=1^2+2^2+3^2+.....+(n-1)^2$
No of rectangles $=1^3+2^3+3^3+.....+(n-1)^3$
So what if we want to calculate the no of all possible quadrilateral? We can choose $4$ points out of $n^2$ points.But in that case, there will be many instances where $3$ or more points will be co-linear.So these will not be a true quadrilateral. So I need to find those combinations of $3$ or more points being co-linear?
References:
How many squares are in the chessboard?
Analysis of how-many-squares and rectangles are are there on a chess board?