PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000378. Nén ma trận nhị phân (Compression)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nén ma trận nhị phân (Compression)

Compression

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a binary matrix AA of size n×nn\times n, whose cells contain only 00 and 11.

A positive integer xx is a valid compression factor if xx divides nn and the matrix can be partitioned into non-overlapping x×xx\times x square blocks within each of which every cell has the same value. Such a block can be represented by a single cell in an (n/x)×(n/x)(n/x)\times(n/x) matrix.

Find the largest possible xx. Compression factor x=1x=1 is always valid.

To save input space, each matrix row is encoded as a hexadecimal string of length n/4n/4. Every character 0–9 or A–F represents exactly four consecutive bits from left to right. For example, B is 1011 and 5 is 0101. There are no spaces within a row.

Input

The first line contains nn, a multiple of 44. The next nn lines each contain one uppercase hexadecimal string of exactly n/4n/4 characters.

Output

Print the maximum valid integer compression factor xx.

Subtasks

  • Subtask 1 (20 points): 4≤n≤404\le n\le40, 4∣n4\mid n; mỗi dòng có đúng n/4n/4 ký tự hex.

  • Subtask 2 (30 points): 4≤n≤6004\le n\le600, 4∣n4\mid n; mỗi dòng có đúng n/4n/4 ký tự hex.

  • Subtask 3 (50 points): 4≤n≤52004\le n\le5200, 4∣n4\mid n; mỗi dòng có đúng n/4n/4 ký tự hex.

Examples

Example 1

Input

8
E7
E7
E7
00
00
E7
E7
E7

Output

1

Explanation

Hex E7 expands to 11100111 and 00 to 00000000. The changes in cell values do not align with boundaries of constant 2×22\times2 blocks; hence the largest factor is 11.

Example 2

Input

4
7
F
F
F

Output

1

Explanation

The first row is 0111, while the remaining rows are 1111. The upper-left 2×22\times2 block contains both 00 and 11, and the entire 4×44\times4 matrix is not constant. Only x=1x=1 works.

Example 3

Input

4
F
F
F
F

Output

4

Explanation

All 16 cells are 11, so the whole matrix forms a constant 4×44\times4 block; the answer is 44.