I got for the number of squares inside x = [0, n] and y = [0, n] /n = 1, 2, 3, . ./ the following:
n*1² + (n-1)*2² + (n-2)*3² + . . + 3*(n-2)² + 2*(n-1)² + 1*n²
For n = 13 there are
13*1² + 12*2² + 11*3² + . . + 3*11² + 2*12² + 1*13² = 3185 squares.
First, let us count how many squares are with sides, parallel to grid lines - one nxn-size, 4=2² (n-1)x(n-1)-size, 9=3² (n-2)x(n-2)-size etc., finally n² 1x1-size
/we have k+1 ways to choose the horizontal and k+1 ways
the vertical projection of (n-k)x(n-k)-size square/.
Next, a square with side √(s'² + s"²) where s' + s" = s ≤ n can be embedded into a sxs-side square of the above type, for example
(1,0), (0,1), (1,2), (2,1).can be embedded into (0,0), (0,2), (2,2), (2,0);
(0,1), (1,3), (3,2), (2,0) and (1,0), (3,1), (2,3) and (0,2)
can be embedded into (0,0), (0,3), (3,3) and (3,0).
If s' = s" the embedding square contains 1 square (connecting its midpoints), otherwise contains 2 squares, symmetric with respect to the line y=x (swapped x and y coordinates). Counting the embedding squares yields the above expression.
(Continued past the weekend) I thought it over and was going to submit further explanation of the above embedding process with the explicit formula and an illustration with the single square for n=1, 6 squares for n=2 and 20 for n=3, but when I came later and read JB's answer with both the formula and picture, I must confess I felt a professional envy (in the good sense of that word).
Superb animation, this is indeed a work of computer art I haven't seen here in Y!A!
Anyway, it's very easy to show that the rectangles with sides, parallel to the grid lines are
n² (n+1)² / 4
That was a question from the early days of MS Windows how many possible widows are on screen with a given resolution - the rectangle structure is described by the 2 diagonally opposite points, so we have n+1 ways to choose the 1st abscissa /0,1, . . , n/, same for the 1st ordinate, then the 2nd point shouldn't be on the same horizontal and vertical grid lines - thus we count every rectangle 4 times.
But to count all rectangles using the embedding approach above is somewhat difficult - the number of the embedded rectangles depends on the sizes, maybe another approach will be more fruitful.
Another difficulties are in the case of cubes. Since the square has 4 sides and 4 vertexes, drawing 4 lines through vertexes, parallel to the coordinate axes, we get an embedding square. But the cube has 8 vertexes and 6 faces and if we try for example to embed the cube, spanned on the vectors
(3, 4, 0), (-4, 3, 0) and (0, 0, 5)
its embedding solid is a cuboid, not a cube, so that approach doesn't look very promising.
I am not sure I'll have enough time (please extend the expiration) to examine this extremely interesting question thoroughly, let's hope JB or somebody else will succeed or will provide some useful links.
(Final edit - Question 2: How Many Rectangles, Including Squares)
2a) As noted above, there are 13² *14² / 4 = 8281 rectangles with sides, parallel to the grid lines;
2b) All other squares and rectangles with sides, multiple of √2 can be embedded into squares of type 2a) with side length 1, 2, . . , 13; the relationship between the side length and number of embedded rectangles is:
side length: 1 2 3 4 5 6 . 7. 8 . 9 10 11 12 13
embedded: 0 1 4 5 8 9 12 13 16 17 20 21 24
The number of all rectangles of type 2b) is then
1*12² + 4*11² + 5*10² + 8*9² + 9*8² + 12*7² + 13*6² + 16*5² + 17*4² + 20*3² + 21*2² + 24*1² = 4368
2c) Rectangles whose embedding rectangle is not a square remain:
embedded embedding number of
rectangle . .rectangle . . embedded
sides . . . . . sides . . . . . rectangles
√5 x 2√5 . . 5 x 4 . . . . . . 4*90
√5 x 3√5 . . 7 x 5 . . . . . . 4*63
√5 x 4√5 . . 9 x 6 . . . . . . 4*40
√5 x 5√5 . . 11 x 7 . . . . . 4*21
√5 x 6√5 . . 13 x 8 . . . . . 4*6
2√5 x 3√5 . . 8 x 7 . . . . . 4*42
2√5 x 4√5 . 10 x 8 . . . . . 4*24
2√5 x 5√5 . 12 x 9 . . . . . 4*10
3√5 x 4√5 . 11 x 10 . . . . 4*12
3√5 x 5√5 . 13 x 11 . . . . 4*3
√10 x 2√10 . 7 x 5 . . . . . 4*63
√10 x 3√10 . 10 x 6 . . . . 4*32
√10 x 4√10 . 13 x 7 . . . . 4*7
2√10 x 3√10 11 x 9 . . . . 4*15
√13 x 2√13 . . 8 x 7 . . . . 4*42
√13 x 3√13 . . 11 x 9 . . . 4*15
2√13 x 3√13 . 13 x 12 . . 4*2
√17 x 2√17 . . 9 x 6 . . . . 4*40
√17 x 3√17 . . 13 x 7 . . . 4*7
5 x 10 . . . . . . . 11 x 10 . . 4*12
√26 x 2√26 . . . 11 x 7 . . . 4*21
√29 x 2√29 . . . 12 x 9 . . . 4*10
√34 x 2√34 . . . 13 x 11 . . 4*3
√37 x 2√37 . . . 13 x 8 . . . 4*6
The side length of the embedded rectangles is a square root of a sum of small naturals from
√(1² + 2²) to √(1² + 6²) /√2 and its multiples included in 2b) above/
The number of embedding a x b rectangles is 2*(14-a)*(14-b) /both a x b and b x a/, each containing 2 embedded (in the 1st column), so the total number of rectangles of type 2c) is the sum of the numbers in the 3rd column above:2344
Total number of rectangles: 8281 + 4368 + 2344 = 14993.
See an illustration of some of above cases:
http://farm4.static.flickr.com/3032/3093095845_2b21f25030_o.gif
Now it is my turn to be happy since this agrees with JB's answer.
Thank You very much for the time You gave me to finish my answer (quite possibly a more efficient enumerating approach would do the job easier) - this was one of the greatest questions I have seen here in Y!A!