The template for an artwork is a white grid of n×m squares. The artwork is finished by painting q horizontal or vertical black strokes on the grid.
A stroke starts at square (x1,y1) and ends at square (x2,y2), where x1=x2 or y1=y2. It turns every square (x,y) with x1≤x≤x2 and y1≤y≤y2 black. Here x is the row index and y is the column index.
The beauty of an artwork is the number of regions in the grid. A region consists of one or more white squares, and any two squares of the same region are connected by a path of white squares that moves up, down, left, or right. Diagonal moves are not allowed. Before any stroke is painted, the beauty is 1.
Compute the beauty of the artwork after each stroke.
The figure below shows how the beauty changes while the strokes of the example are painted.

Figure 1: the example, stroke by stroke.