Euler Diagrams

An Euler diagram (named after Leonhard Euler) consists of simple closed curves in the plane, usually circles, that depict sets. The spatial relationships between the regions bounded by each curve (overlap, containment or neither) corresponds to set-theoretic relationships (intersection, subset and disjointness, respectively); depending on the relative location and size of the curves, the plane (or, as is usually the case, a paper sheet) is divided in a certain number of zones, each one of which represents an intersection of the original sets or their complements. A more restrictive form of Euler diagrams are Venn diagrams, which must include all logically possible zones of overlap between its curves. Formally, given circular regions S1,S2,...,Sn in the plane, we shall define a zone as a nonempty set of the form f1(S1) ∩ f2(S2) ∩ · · · ∩ fn(Sn), where, for each i, either fi(Si) = Si or fi(Si) = Sic (the complement of Si with respect to the drawing surface). Given a rectangular drawing surface and a collection of circles, find the number of zones in which the surface is split. Note that, in the last example, zone 2 is labeled twice even though both labels are in the same set. Input The input consists of several test cases. Each case begins with three blank-separated positive integers, W, H and n, which represent, respectively, the width of the drawing surface, the height of the drawing surface, and the number of circles in the diagram (8 ≤ W ≤ 64, 8 ≤ H ≤ 64 and 0 ≤ n ≤ 8). Each one of the next n lines consists of three blank-separated positive integers, x, y and r, specifying the center (x, y) and radius r of a circle (0 ≤ x ≤ W , 0 ≤ y ≤ H, and 1 ≤ r ≤ W + H). You may assume every circle is fully contained within the drawing surface, that no two circles intersect at a single point, that every two circles are different, and that the sides of the surface are not tangent to any circle. The end of the input is given by W = H = n = 0, which should not be processed as a test case. Output For every test case print a line with the number of zones in which the drawing surface was split by the circles. Sample Input 60 44 3 12 14 10 24 32 10 48 26 10 60 44 3 16 16 10

2/2 34 30 10 44 22 10 60 44 3 24 16 10 28 28 10 36 20 10 60 44 3 30 22 20 20 22 16 40 22 16 50 50 4 25 25 5 25 25 10 25 25 15 25 25 20 50 50 3 25 25 5 25 25 10 25 25 15 50 50 2 25 25 5 25 25 10 50 50 1 25 25 5 50 50 0 50 50 5 15 25 6 20 25 6 25 25 6 30 25 6 35 25 6 50 50 3 25 35 10 15 25 9 35 25 9 000 Sample Output 4 5 8 7 5 4 3 2 1 13 6