It’s a question that has appeared often in “math trick” books – an image formed by a grid of lines is presented, and the reader is asked to count the number of squares. Here’s one that I recently saw on Facebook:

The answer expected by most “math trick” books is 40. Click on this icon to see an animation of how to arrive at this answer:

But mathematically speaking, this is not the correct answer. The reason is because the definition of a square – “a regular quadrilateral” [1] cannot be accurately represented by your computer screen, which is made of pixels that have width and height (and which themselves do not physically touch, nor might they actually be square!). For that matter, nor can a mathematical square be accurately represented on a piece of paper, because even the line made by the finest of fine-point pens has thickness. That means that in the very least, for any physically-drawn object, there are a minimum of TWO mathematical squares: the “inner square” formed by the inner edge of the border, and the “outer square” formed by the outer edge of the border.

Here’s a simpler demonstration of what I mean. How many squares are in the following image?

The obvious answer is “one” – the image is that of a square. But realizing that the black portion of the image has thickness, there are actually at least TWO squares. There is the “outer edge” of the black square and the “inner edge square” of the black square, which could also be described as the outer edge of the inner white square. In the image below, I’ve highlighted both in blue/yellow below (look closely!) Of course, even this isn’t really correct because the highlights that I’ve drawn also have thickness, but for the sake of demonstration, realize that the blue/yellow is only meant to highlight the areas I’m referring to.

But it gets worse. The reason there is an “outer edge” square is because we are assuming the background to be white (the color of the this page). This is more clearly demonstrated by the following example. If we assume a shape to be defined as the border (or “contrast”) between a foreground color and a background color, then the question becomes a bit more simple. So for example, I might ask how many squares you see in each of the two following images:

The answer to this first image is obviously 4. (Some people might argue that there are 5, the 5th square being formed by the border of full image, but that is nothing more than an optical illusion – there is no mathematical support whatsoever for the “5” answer.) But the reason you see 4 squares here is because the background of this web page is white, and I’ve tricked you by including a “hidden” border around the image that is the same as the background of the web page – white. Here is the ** exact** same image, but with the colors inverted:

Now that the “background” doesn’t match the border, you can see it, and by following the intention of the original problem, the answer would be that there are 5 squares (although for a slightly different reason than the optical illusion argument in the previous example). ** But why should simply changing the color of the image alter the number of squares it contains??** It shouldn’t, and it doesn’t! This is simply another effect of the inability of paper or computer screens to accurately represent strict geometrical figures. It’s what I’ll call the “foreground / background” problem and is due to the fact that we usually have to assume a particular “background” color.

In fact, it should be clear that the second image is just a smaller version of the original problem. Where most “math trick” books would expect the answer of 5, the more accurate answer would be obtained by inverting the image, and then counting the “filled squares” of the result (as we did with the first image). In order for the “contrast” method to be used, we are justified in treating the geometrical shapes as filled (solid, instead of simple outlines) because a square as mathematically defined says nothing about the interior. Furthermore, realize that when you are counting the solid squares, what you are really counting is a more graphically-obvious object that is meant to represent the border, which is the true square. In fact, it is mathematically more accurate this way, since the border of the two colors is as close as we can get on paper / screen to the mathematical concept of a line that would truly define a geometric shape.

This now allows us to take another look at the original image in a different light. Let’s invert the original image and re-count the squares:

NOW how many squares are there? Note that this is exactly the same problem as originally presented at the top of this page, but now using the improved method of defining a geometric shape as the intersection between two colors that is more effective when presented on paper / screen, and we can perform a better count of how many squares there actually are. In this refined image, we should be counting the “solid black” squares, since the background is defined as “white”.

There are two basic shapes in this image: squares and “L”s. There are, in fact, 4 large squares along the left, 4 large squares along the right, and 8 small squares arranged in 2 groups of 4, each group of 4 surrounded by 4 “L”s with different rotations. Therefore, the true “mathematically graphical” answer to the original problem is that there are SIXTEEN squares:

This answer takes into account and eliminates the foreground/background issue as well as the “thickness” issue, but is not the answer that you’ll see in most books. For that matter, if you ever take an IQ test and see a problem like this, I doubt that they would be expecting this answer either, so don’t overthink it.

[1] Wikipedia, “Square (geometry)”, 7/29/2012. http://en.wikipedia.org/wiki/Square_(geometry)