Count the Number of Squares

More Details

In a 3×5 grid, there are three varieties of squares:

1. Small, upright squares

2. Larger, upright squares

3. Squares at a 45° angle

Carefully counting all of these kinds of squares reveals that there are 14 in total.

The same square-counting exercise could be performed even if some of the points in this 3×5 grid were missing, but there would be fewer squares. Of course, in larger grids, even larger square sizes can appear.

In this Challenge, all grids or subsets of grids are represented as a list of integer pairs. The vertices in the 3×5 rectangular grid above can be written as follows:

Tuples[{Range[3],Range[5]}]

What Your Function Should Do

Create a function CountSquares that takes as input a subset of points in a rectangular grid represented as a list of integer pairs. The output of your function should be the number of squares with vertices among those points. In other words, you should return the number of squares you can draw with edges between the points given. Make sure your function also counts squares that are positioned at an angle.

CountSquares[Tuples[Range[3],2]]

Here are all 6 squares visualized:

More Examples

CountSquares[{{4,3},{1,1},{5,3},{2,3},{3,2},{3,1},{4,2},{2,1},{3,3},{1,2},{5,2}}]

Here are the 3 squares in this set:

CountSquares[{{4,3},{4,2},{3,1},{6,1},{7,4},{4,4},{3,3},{5,4},{7,1},{3,4},{5,2},{5,1},{1,1},{2,3},{2,4}}]

Here are the 3 squares in this set:

CountSquares[{{3,4},{1,2},{3,2},{4,5},{4,2},{5,3},{4,1},{5,4},{3,5},{2,4},{2,2},{1,1},{4,4},{2,5},{1,5},{2,1},{2,3},{4,3}}]

Here are the 15 squares in this set:

CountSquares[pts_List] :=