Quadtree Image Compression

No attempts yetTime limit2sMemory limit1024 MB

Problem

Compute the output length of the quadtree image compression scheme. The image is an L×LL \times L square of L2L^2 pixels, and every pixel is either a 00-pixel or a 11-pixel.

The scheme works like this.

  1. If the image contains both 00-pixels and 11-pixels, write a single 11 to record that the image is split into 44 sub-images by rule 2. If the image contains only one kind of pixel, write 0000 when every pixel is 00 and 0101 when every pixel is 11, then stop.
  2. Split image II into 44 sub-images AA, BB, CC, DD of equal size as shown below. Then apply rule 1 to each sub-image in the order AA, BB, CC, DD.

Diagram showing where sub-images A, B, C, D sit inside the image

The length of the compressed image is the number of bits in its encoding. LL is a power of 22 and 1L641 \le L \le 64.

Input

The first line contains the integer LL. Each of the next LL lines contains LL bits, with a single blank between two adjacent bits. Every bit is 00 or 11.

Output

Print the length of the compressed image, that is, the number of bits, on one line.