You are given a binary matrix $A$ of size $n \times n$. Let's denote an $x$-compression of the given matrix as a matrix $B$ of size $\frac{n}{x} \times \frac{n}{x}$ such that for every $i \in [1, n], j \in [1, n]$ the condition $A[i][j] = B[\lceil \frac{i}{x} \rceil][\lceil \frac{j}{x} \rceil]$ is met.

Obviously, $x$-compression is possible only if $x$ divides $n$, but this condition is not enough. For example, the following matrix of size $2 \times 2$ does not have any $2$-compression:

 $01$   $10$ 

For the given matrix $A$, find maximum $x$ such that an $x$-compression of this matrix is possible.

Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.


-----Input-----

The first line contains one number $n$ ($4 \le n \le 5200$) — the number of rows and columns in the matrix $A$. It is guaranteed that $n$ is divisible by $4$.

Then the representation of matrix follows. Each of $n$ next lines contains $\frac{n}{4}$ one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from $0$ to $9$ or as uppercase Latin letters from $A$ to $F$). Binary representation of each of these numbers denotes next $4$ elements of the matrix in the corresponding row. For example, if the number $B$ is given, then the corresponding elements are 1011, and if the number is $5$, then the corresponding elements are 0101.

Elements are not separated by whitespaces.


-----Output-----

Print one number: maximum $x$ such that an $x$-compression of the given matrix is possible.


-----Examples-----
Input
8
E7
E7
E7
00
00
E7
E7
E7

Output
1

Input
4
7
F
F
F

Output
1



-----Note-----

The first example corresponds to the matrix:  $11100111$   $11100111$   $11100111$   $00000000$   $00000000$   $11100111$   $11100111$   $11100111$ 

It is easy to see that the answer on this example is $1$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Mathematics

Number theory

Dynamic programming

Implementation


Solutions:
import sys
hex2bin = [''] * 256
hex2bin[ord('0')] = '0000'
hex2bin[ord('1')] = '0001'
hex2bin[ord('2')] = '0010'
hex2bin[ord('3')] = '0011'
hex2bin[ord('4')] = '0010'
hex2bin[ord('5')] = '0010'
hex2bin[ord('6')] = '0110'
hex2bin[ord('7')] = '0111'
hex2bin[ord('8')] = '1000'
hex2bin[ord('9')] = '1001'
hex2bin[ord('A')] = '0010'
hex2bin[ord('B')] = '0010'
hex2bin[ord('C')] = '1100'
hex2bin[ord('D')] = '0010'
hex2bin[ord('E')] = '1110'
hex2bin[ord('F')] = '1111'
n = int(input())
buckets = [0] * (n + 1)
inp = sys.stdin.read().splitlines()
prev = ''
count = 0
for i in range(n):
	if inp[i] == prev:
		count += 1
	else:
		buckets[count] += 1
		count = 1
		prev = inp[i]
		prev_c = ''
		counter = 0
		for hexx in prev:
			for c in hex2bin[ord(hexx)]:
				if c == prev_c:
					counter += 1
				else:
					buckets[counter] += 1
					counter = 1
					prev_c = c
		buckets[counter] += 1
	if buckets[1] != 0 or (buckets[2] != 0 and buckets[3] != 0):
		print(1)
		return
buckets[count] += 1
x = 0
for i in range(1, n + 1):
	if buckets[i]:
		while i:
			(i, x) = (x % i, i)
print(x)


(lambda R: (lambda N: (lambda n: any(((lambda z, r: all(((lambda p: all((p == k for k in n[j:j + i])))(n[j]) for j in range(0, N, i))) and all((all(((lambda q: q == z or q == r)(j[k:k + i]) for k in range(0, N, i))) for j in n)) and [print(i)])('0' * i, '1' * i) for i in range(N, 0, -1) if N % i == 0)))([bin(int(R(), 16))[2:].zfill(N) for i in range(N)]))(int(R())))(__import__('sys').stdin.readline)


from math import gcd
n = int(input())
a = [[2] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	j = 0
	for x in map(lambda c: int(c, 16), input()):
		for d in range(3, -1, -1):
			a[i][j] = 1 if 1 << d & x else 0
			j += 1
	a[i][-1] = 2
a[-1] = [2] * (n + 1)
ans = 0
for i in range(n):
	(prev, cont) = (a[i][0], 1)
	for j in range(1, n + 1):
		if prev != a[i][j]:
			if ans == 0:
				ans = cont
			else:
				ans = gcd(ans, cont)
			(prev, cont) = (a[i][j], 1)
		else:
			cont += 1
for j in range(n):
	(prev, cont) = (a[0][j], 1)
	for i in range(1, n + 1):
		if prev != a[i][j]:
			ans = gcd(ans, cont)
			(prev, cont) = (a[i][j], 1)
		else:
			cont += 1
print(ans)


import os
from atexit import register
from io import BytesIO
stdout = BytesIO()
register(lambda : os.write(1, stdout.getvalue()))
readline = BytesIO(os.read(0, os.fstat(0).st_size)).readline
input = lambda : readline().decode().rstrip('\r\n')

def print(*args, sep=' ', end='\n'):
	stdout.write(sep.join(map(str, args)).encode())
	stdout.write(end.encode())

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def main():
	n = int(input())
	mat = [format(int(input(), 16), '0%db' % n) for _ in range(n)]
	t = [0] * (n + 1)
	for i in range(n):
		(cnt1, cnt2) = (1, 1)
		(prev1, prev2) = (mat[i][0], mat[0][i])
		for j in range(1, n):
			if mat[i][j] == prev1:
				cnt1 += 1
			else:
				(t[cnt1], cnt1) = (1, 1)
			if mat[j][i] == prev2:
				cnt2 += 1
			else:
				(t[cnt2], cnt2) = (1, 1)
			(prev1, prev2) = (mat[i][j], mat[j][i])
		(t[cnt1], t[cnt2]) = (1, 1)
	res = n
	for i in range(n):
		if t[i] == 1:
			res = gcd(i, res)
	print(res)
main()


from math import gcd
from functools import reduce
import sys
import os
d = sys.stdin.readlines()
if d[-1][-1] != '\n':
	d[-1] += '\n'
n = int(d[0])
cur_tab = [0 for _ in range(n + 1)]

def group(l):
	cur = 1
	for x in range(1, n):
		if l[x] == l[x - 1]:
			cur += 1
		else:
			cur_tab[cur] = 1
			cur = 1
	cur_tab[cur] = 1
mat = [None for _ in range(n)]
euh = 0
for x in d[1:]:
	line = x[:-1]
	s = str(bin(int(line, 16)))[2:].zfill(n)
	mat[euh] = s
	euh += 1
	group(s)
for i in range(n):
	cur = 1
	for x in range(1, n):
		if mat[x][i] == mat[x - 1][i]:
			cur += 1
		else:
			cur_tab[cur] = 1
			cur = 1
	cur_tab[cur] = 1
print(reduce(gcd, [index for index in range(n + 1) if cur_tab[index] == 1]))


from sys import stdin
n = int(input())
matrix = stdin.read().splitlines()
matrix = [bin(int(i, 16))[2:].zfill(n) for i in matrix]
dp = [[0] * (n + 1) for _ in range(n + 1)]
for y in range(1, n + 1):
	for x in range(1, n + 1):
		dp[y][x] = dp[y - 1][x] + dp[y][x - 1] - dp[y - 1][x - 1] + (matrix[y - 1][x - 1] == '1')

def get_sum(sy, sx, ey, ex):
	return dp[ey][ex] - dp[ey][sx] - dp[sy][ex] + dp[sy][sx]
ans = 1
for x in range(n, 0, -1):
	if n % x == 0:
		flag = True
		for i in range(0, n, x):
			for j in range(0, n, x):
				temp = get_sum(i, j, i + x, j + x)
				if temp not in (x ** 2, 0):
					flag = False
					break
			if not flag:
				break
		if flag:
			ans = x
			break
print(ans)


from math import gcd
import sys
n = int(sys.stdin.readline())
a = [[] for i in range(n)]
for i in range(n):
	a[i] = bin(1 << n | int(sys.stdin.readline(), 16))[3:]
(ans, i) = (0, 0)
while i < n:
	j = i + 1
	while j < n and a[i] == a[j]:
		j += 1
	ans = gcd(ans, j - i)
	k = 0
	while k < n:
		l = k + 1
		while l < n and a[i][k] == a[i][l]:
			l += 1
		ans = gcd(ans, abs(l - k))
		k = l
	i = j
print(ans)


import sys
from math import gcd
z = n = int(input())
a = [bin(1 << n | int(input(), 16))[3:] for i in range(n)]
i = 0
while i < n:
	j = i + 1
	while j < n and a[i] == a[j]:
		j += 1
	z = gcd(z, j - i)
	k = 0
	while k < n:
		l = k + 1
		while l < n and a[i][k] == a[i][l]:
			l += 1
		z = gcd(z, l - k)
		k = l
	i = j
print(z)


from math import gcd
from functools import reduce
import sys
import os
d = sys.stdin.readlines()

class Tab(object):

	def __init__(self):
		self.value = [0 for _ in range(n + 1)]

	def set_one(self, ind):
		if ind == 1:
			print(1)
			exit(0)
		else:
			self.value[ind] = 1

	def ones(self):
		return [index for index in range(n + 1) if self.value[index] == 1]
n = int(d[0])
cur_tab = Tab()

def group(l):
	cur = 1
	for x in range(1, n):
		if l[x] == l[x - 1]:
			cur += 1
		else:
			cur_tab.set_one(cur)
			cur = 1
	cur_tab.set_one(cur)
mat = [None for _ in range(n)]
euh = 0
for x in d[1:]:
	line = x[:-1]
	s = str(bin(int(line, 16)))[2:].zfill(n)
	mat[euh] = s
	euh += 1
	group(s)
for i in range(n):
	cur = 1
	for x in range(1, n):
		if mat[x][i] == mat[x - 1][i]:
			cur += 1
		else:
			cur_tab.set_one(cur)
			cur = 1
	cur_tab.set_one(cur)
print(reduce(gcd, cur_tab.ones()))


import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
from heapq import *
from math import gcd

def main():
	n = int(input())
	D = []
	for i in range(n):
		x = bin(int(input(), 16))[2:]
		D.append('0' * (n - len(x)) + x)
	row = []
	length = 1
	for i in range(1, n):
		if D[i - 1] == D[i]:
			length += 1
		else:
			row.append(length)
			length = 1
	row.append(length)
	col = []
	length = 1
	for i in range(1, n):
		flag = True
		for j in range(n):
			if D[j][i - 1] != D[j][i]:
				flag = False
		if flag:
			length += 1
		else:
			col.append(length)
			length = 1
	col.append(length)
	ans1 = row[0]
	for x in row:
		ans1 = gcd(ans1, x)
	ans2 = col[0]
	for y in col:
		ans2 = gcd(ans2, y)
	print(gcd(ans1, ans2))
main()


from sys import stdin
import math
n = int(input())
matrix = stdin.read().splitlines()
grid = [bin(int(i, 16))[2:].zfill(n) for i in matrix]
dx = [True] * n
dy = [True] * n
for i in range(n - 1):
	if grid[i] != grid[i + 1]:
		dy[i] = False
for i in range(n - 1):
	flag = True
	for j in range(n):
		if grid[j][i] != grid[j][i + 1]:
			flag = False
			break
	dx[i] = flag
ans = 1
for x in range(n, 0, -1):
	if n % x == 0:
		flag = True
		for i in range(n):
			if i % x == x - 1:
				continue
			if not dx[i] or not dy[i]:
				flag = False
				break
		if flag:
			ans = x
			break
print(ans)


import sys
import math

def compression(n, matrix):
	i = ans = 0
	while i < n:
		j = i + 1
		while j < n and matrix[i] == matrix[j]:
			j += 1
		ans = math.gcd(ans, j - i)
		col = 0
		while col < n:
			l = col + 1
			while l < n and matrix[i][col] == matrix[i][l]:
				l += 1
			ans = math.gcd(ans, col - l)
			col = l
		i = j
	return ans
n = int(sys.stdin.readline())
matrix = [[] for _ in range(n)]
for i in range(n):
	matrix[i] = bin(1 << n | int(sys.stdin.readline(), 16))[3:]
print(compression(n, matrix))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=-10 ** 6, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(50001)]
pp = []

def SieveOfEratosthenes(n=50000):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for i in range(50001):
		if prime[i]:
			pp.append(i)
ind = defaultdict(list)
ind['1'] = [0, 0, 0, 1]
ind['0'] = [0, 0, 0, 0]
ind['2'] = [0, 0, 1, 0]
ind['3'] = [0, 0, 1, 1]
ind['4'] = [0, 1, 0, 0]
ind['5'] = [0, 1, 0, 1]
ind['6'] = [0, 1, 1, 0]
ind['7'] = [0, 1, 1, 1]
ind['8'] = [1, 0, 0, 0]
ind['9'] = [1, 0, 0, 1]
ind['A'] = [1, 0, 1, 0]
ind['B'] = [1, 0, 1, 1]
ind['C'] = [1, 1, 0, 0]
ind['D'] = [1, 1, 0, 1]
ind['E'] = [1, 1, 1, 0]
ind['F'] = [1, 1, 1, 1]
n = int(input())
l = [[] for i in range(n)]
for i in range(n):
	s = input()
	for j in s:
		for u in ind[j]:
			l[i].append(u)
a = []
c = 1
for i in range(1, n):
	f = 0
	for j in range(n):
		if l[i - 1][j] != l[i][j]:
			f = 1
			a.append(c)
			c = 1
			break
	if f == 0:
		c += 1
a.append(c)
c = 1
for i in range(1, n):
	f = 0
	for j in range(n):
		if l[j][i - 1] != l[j][i]:
			f = 1
			a.append(c)
			c = 1
			break
	if f == 0:
		c += 1
a.append(c)
ans = 0
for i in a:
	ans = math.gcd(i, ans)
print(ans)


import math
ans = 0

def string(s, n):
	j = ''
	for i in range(n // 4):
		if s[i] == '0':
			j = j + '0000'
		elif s[i] == '1':
			j = j + '0001'
		elif s[i] == '2':
			j = j + '0010'
		elif s[i] == '3':
			j = j + '0011'
		elif s[i] == '4':
			j = j + '0100'
		elif s[i] == '5':
			j = j + '0101'
		elif s[i] == '6':
			j = j + '0110'
		elif s[i] == '7':
			j = j + '0111'
		elif s[i] == '8':
			j = j + '1000'
		elif s[i] == '9':
			j = j + '1001'
		elif s[i] == 'A':
			j = j + '1010'
		elif s[i] == 'B':
			j = j + '1011'
		elif s[i] == 'C':
			j = j + '1100'
		elif s[i] == 'D':
			j = j + '1101'
		elif s[i] == 'E':
			j = j + '1110'
		elif s[i] == 'F':
			j = j + '1111'
	return j
n = int(input(''))
a = []
x = 1
ansx = set()
ansy = set()
for i in range(n):
	s = input('')
	a.append(string(s, n))
j = 0
k = a[0]
c = 1
for i in range(1, n):
	if k == a[i]:
		c += 1
		if i == n - 1:
			ansy.add(c)
	else:
		if c == 1:
			print('1')
			exit()
		ansy.add(c)
		c = 1
		k = a[i]
for i in range(n):
	k = a[i][0]
	c = 1
	for j in range(1, n):
		if k == a[i][j]:
			c += 1
			if j == n - 1:
				ansx.add(c)
		else:
			if c == 1:
				print('1')
				exit()
			ansx.add(c)
			k = a[i][j]
			c = 1
ansx = list(ansx)
ansy = list(ansy)
my = min(ansy)
mx = min(ansx)
ans = 0
for i in range(len(ansx)):
	if math.gcd(ansx[i], mx) == 1:
		ans = -1
if ans == 0:
	for i in range(len(ansy)):
		if math.gcd(ansy[i], my) == 1:
			ans = -1
if ans == -1:
	print('1')
else:
	print(math.gcd(mx, my))


from sys import stdin, stdout

def hexa(c):
	if c == '0':
		return '0000'
	if c == '1':
		return '0001'
	if c == '2':
		return '0010'
	if c == '3':
		return '0011'
	if c == '4':
		return '0100'
	if c == '5':
		return '0101'
	if c == '6':
		return '0110'
	if c == '7':
		return '0111'
	if c == '8':
		return '1000'
	if c == '9':
		return '1001'
	if c == 'A':
		return '1010'
	if c == 'B':
		return '1011'
	if c == 'C':
		return '1100'
	if c == 'D':
		return '1101'
	if c == 'E':
		return '1110'
	if c == 'F':
		return '1111'

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
n = int(stdin.readline())
a = [0] * n
for _ in range(n):
	s = stdin.readline().strip()
	t = ''
	for i in s:
		t += hexa(i)
	a[_] = t
r = []
t = 1
for i in range(1, n):
	if a[i] == a[i - 1]:
		t += 1
	else:
		r.append(t)
		t = 1
r.append(t)
c = []
t = 1
for i in range(1, n):
	f = 1
	for j in range(n):
		if a[j][i - 1] != a[j][i]:
			f = 0
			break
	if f == 1:
		t += 1
	else:
		c.append(t)
		t = 1
c.append(t)
ans = r[0]
for i in r + c:
	ans = gcd(ans, i)
stdout.write('%d' % ans)


from sys import stdin
import math
n = int(input())
matrix = stdin.read().splitlines()
hex2bin_map = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
grid = [''.join((hex2bin_map[i] for i in line)) for line in matrix]
dx = [True] * n
dy = [True] * n
for i in range(n - 1):
	if grid[i] != grid[i + 1]:
		dy[i] = False
for i in range(n - 1):
	flag = True
	for j in range(n):
		if grid[j][i] != grid[j][i + 1]:
			flag = False
			break
	dx[i] = flag
ans = 1
for x in range(n, 0, -1):
	if n % x == 0:
		flag = True
		for i in range(n):
			if i % x == x - 1:
				continue
			if not dx[i] or not dy[i]:
				flag = False
				break
		if flag:
			ans = x
			break
print(ans)


n = int(input())
MAT = [format(int(input(), 16), 'b').zfill(n) for i in range(n)]
R = []
NOW = 1
for i in range(1, n):
	if MAT[i] == MAT[i - 1]:
		NOW += 1
	else:
		R.append(NOW)
		NOW = 1
R.append(NOW)
C = []

def matchcol(i):
	for j in range(n):
		if MAT[j][i - 1] != MAT[j][i]:
			return 0
	else:
		return 1
NOW = 1
for i in range(1, n):
	if matchcol(i):
		NOW += 1
	else:
		C.append(NOW)
		NOW = 1
C.append(NOW)

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
ANS = R[0]
for x in R + C:
	ANS = gcd(ANS, x)
print(ANS)


from __future__ import division, print_function
import cmath
import itertools
import math
import operator as op
import sys
from atexit import register
from bisect import bisect_left, bisect_right
from io import BytesIO
if sys.version_info[0] < 3:
	pass
else:
	pass
if sys.version_info[0] < 3:

	class dict(dict):

		def items(self):
			return dict.iteritems(self)

		def keys(self):
			return dict.iterkeys(self)

		def values(self):
			return dict.itervalues(self)
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def sync_with_stdio(sync=True):
	global input, flush
	if sync:
		flush = sys.stdout.flush
	elif sys.version_info[0] < 3:
		sys.stdin = BytesIO(sys.stdin.read())
		input = lambda : sys.stdin.readline().rstrip('\r\n')
		sys.stdout = BytesIO()
		register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
	else:
		input = iter(sys.stdin.buffer.read().splitlines()).__next__
		print = lambda *args: sys.stdout.buffer.write((str(arg).encode('ascii') for arg in args))

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	hex2bin = [''] * 256
	hex2bin[ord('0')] = b'0000'
	hex2bin[ord('1')] = b'0001'
	hex2bin[ord('2')] = b'0010'
	hex2bin[ord('3')] = b'0011'
	hex2bin[ord('4')] = b'0100'
	hex2bin[ord('5')] = b'0101'
	hex2bin[ord('6')] = b'0110'
	hex2bin[ord('7')] = b'0111'
	hex2bin[ord('8')] = b'1000'
	hex2bin[ord('9')] = b'1001'
	hex2bin[ord('A')] = b'1010'
	hex2bin[ord('B')] = b'1011'
	hex2bin[ord('C')] = b'1100'
	hex2bin[ord('D')] = b'1101'
	hex2bin[ord('E')] = b'1110'
	hex2bin[ord('F')] = b'1111'
	n = int(input())
	buckets = [0] * (n + 1)
	prev = b''
	count = 0
	for _ in range(n):
		line = input()
		if line == prev:
			count += 1
		else:
			buckets[count] += 1
			count = 1
			prev = line
			prev_c = b''
			counter = 0
			for byte in prev:
				for c in hex2bin[byte]:
					if c == prev_c:
						counter += 1
					else:
						buckets[counter] += 1
						counter = 1
						prev_c = c
			buckets[counter] += 1
		if buckets[1] != 0 or (buckets[2] != 0 and buckets[3] != 0):
			print(1)
			sys.exit()
	buckets[count] += 1
	x = 0
	for i in range(1, n + 1):
		if buckets[i]:
			while i:
				(i, x) = (x % i, i)
	print(x)
sync_with_stdio(False)
main()


import math
n = int(input())
arr = []
for i in range(n):
	x = bin(int(input(), 16))[2:]
	arr.append('0' * (n - len(x)) + x)
arr.append('fe')
count = 1
mini = 0
store1 = []
for i in range(n):
	if arr[i] == arr[i + 1]:
		count += 1
	else:
		store1.append(count)
		count = 1
store2 = []
length = 1
for i in range(1, n):
	flag = True
	for j in range(n):
		if arr[j][i - 1] != arr[j][i]:
			flag = False
	if flag:
		length += 1
	else:
		store2.append(length)
		length = 1
store2.append(length)
ans1 = store1[0]
for x in store1:
	ans1 = math.gcd(ans1, x)
ans2 = store2[0]
for y in store2:
	ans2 = math.gcd(ans2, y)
print(math.gcd(ans1, ans2))


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
n = int(input())
ans = n
c = 0
pre = ''
for i in range(n):
	s = bin((1 << n) + int(input(), 16))[3:]
	if i == 0 or s != pre:
		pre = s
		ans = gcd(ans, c)
		c = 1
		cc = 1
		for j in range(1, n):
			if s[j] == s[j - 1]:
				cc += 1
			else:
				ans = gcd(ans, cc)
				cc = 1
	else:
		c += 1
ans = gcd(ans, c)
print(ans)


from math import gcd
n = int(input())
a = []
for i in range(n):
	row = input()
	a.append(bin(int(row, 16))[2:].zfill(n))
to_vis = [0] * n
ans = n
for i in range(n):
	br = False
	j = to_vis[i]
	while j < n:
		currx = 1
		d = a[i][j]
		expand = True
		while expand and currx + i < n and (currx + j < n):
			for ki in range(i, i + currx + 1):
				if a[ki][j + currx] != d:
					expand = False
					break
			if expand:
				for kj in range(j, j + currx + 1):
					if a[i + currx][kj] != d:
						expand = False
						break
			if expand:
				currx += 1
		for ki in range(i, i + currx):
			if to_vis[ki] == j:
				to_vis[ki] = j + currx
		ans = gcd(ans, currx)
		if ans == 1:
			br = True
			break
		j += currx
	if br:
		break
print(ans)


import sys, os

def gcd(a, b):
	if b == 0:
		return a
	while a > 0:
		(a, b) = (b % a, a)
	return b
hex2bin = [''] * 256
hex2bin[ord('0')] = b'0000'
hex2bin[ord('1')] = b'0001'
hex2bin[ord('2')] = b'0010'
hex2bin[ord('3')] = b'0011'
hex2bin[ord('4')] = b'0100'
hex2bin[ord('5')] = b'0101'
hex2bin[ord('6')] = b'0110'
hex2bin[ord('7')] = b'0111'
hex2bin[ord('8')] = b'1000'
hex2bin[ord('9')] = b'1001'
hex2bin[ord('A')] = b'1010'
hex2bin[ord('B')] = b'1011'
hex2bin[ord('C')] = b'1100'
hex2bin[ord('D')] = b'1101'
hex2bin[ord('E')] = b'1110'
hex2bin[ord('F')] = b'1111'
inp = os.read(0, os.fstat(0).st_size).splitlines()
n = int(inp[0])
buckets = [0] * (n + 1)
prev = b''
count = 0
for ind in range(n):
	line = inp[ind + 1]
	if line == prev:
		count += 1
	else:
		buckets[count] += 1
		count = 1
		prev = line
		prev_c = b''
		counter = 0
		for byte in prev:
			for c in hex2bin[byte]:
				if c == prev_c:
					counter += 1
				else:
					buckets[counter] += 1
					counter = 1
					prev_c = c
		buckets[counter] += 1
	if buckets[1] != 0 or (buckets[2] != 0 and buckets[3] != 0):
		print(1)
		sys.exit()
buckets[count] += 1
x = 0
for i in range(1, n + 1):
	if buckets[i]:
		while i:
			(i, x) = (x % i, i)
print(x)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
ind = defaultdict(list)
ind['1'] = [0, 0, 0, 1]
ind['0'] = [0, 0, 0, 0]
ind['2'] = [0, 0, 1, 0]
ind['3'] = [0, 0, 1, 1]
ind['4'] = [0, 1, 0, 0]
ind['5'] = [0, 1, 0, 1]
ind['6'] = [0, 1, 1, 0]
ind['7'] = [0, 1, 1, 1]
ind['8'] = [1, 0, 0, 0]
ind['9'] = [1, 0, 0, 1]
ind['A'] = [1, 0, 1, 0]
ind['B'] = [1, 0, 1, 1]
ind['C'] = [1, 1, 0, 0]
ind['D'] = [1, 1, 0, 1]
ind['E'] = [1, 1, 1, 0]
ind['F'] = [1, 1, 1, 1]
n = int(input())
l = [[] for i in range(n)]
for i in range(n):
	s = input()
	for j in s:
		for u in ind[j]:
			l[i].append(u)
a = []
c = 1
for i in range(1, n):
	f = 0
	for j in range(n):
		if l[i - 1][j] != l[i][j]:
			f = 1
			a.append(c)
			c = 1
			break
	if f == 0:
		c += 1
a.append(c)
c = 1
for i in range(1, n):
	f = 0
	for j in range(n):
		if l[j][i - 1] != l[j][i]:
			f = 1
			a.append(c)
			c = 1
			break
	if f == 0:
		c += 1
a.append(c)
ans = 0
for i in a:
	ans = math.gcd(i, ans)
print(ans)


from sys import stdin, stdout

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
n = int(stdin.readline())
temp = ''
ans = n
p = 0
for i in range(n):
	s = bin((1 << n) + int(stdin.readline(), 16))[3:]
	if i == 0 or temp != s:
		temp = s
		ans = gcd(ans, p)
		p = 1
		pp = 1
		for j in range(1, n):
			if s[j] == s[j - 1]:
				pp += 1
			else:
				ans = gcd(ans, pp)
				pp = 1
	else:
		p += 1
ans = gcd(ans, p)
print(ans)


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
from heapq import *
from math import gcd

def main():
	n = int(input())
	D = []
	for i in range(n):
		x = bin(int(input(), 16))[2:]
		D.append('0' * (n - len(x)) + x)
	row = []
	length = 1
	for i in range(1, n):
		if D[i - 1] == D[i]:
			length += 1
		else:
			row.append(length)
			length = 1
	row.append(length)
	col = []
	length = 1
	for i in range(1, n):
		flag = True
		for j in range(n):
			if D[j][i - 1] != D[j][i]:
				flag = False
		if flag:
			length += 1
		else:
			col.append(length)
			length = 1
	col.append(length)
	ans1 = row[0]
	for x in row:
		ans1 = gcd(ans1, x)
	ans2 = col[0]
	for y in col:
		ans2 = gcd(ans2, y)
	print(gcd(ans1, ans2))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
l = [format(int(input(), 16), 'b').zfill(n) for i in range(n)]
row = []
col = []

def cm(j):
	for i in range(n):
		if l[i][j - 1] != l[i][j]:
			return False
	return True

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
cnt = 1
for i in range(1, n):
	if l[i] == l[i - 1]:
		cnt += 1
	else:
		row.append(cnt)
		cnt = 1
row.append(cnt)
cnt = 1
for j in range(1, n):
	if cm(j):
		cnt += 1
	else:
		col.append(cnt)
		cnt = 1
col.append(cnt)
ans = col[0]
for i in row + col:
	ans = gcd(i, ans)
print(ans)


import math
d = {}
for i in range(0, 16):
	if i >= 10:
		d[chr(55 + i)] = bin(i).replace('0b', '')
	else:
		d[str(i)] = (4 - len(bin(i).replace('0b', ''))) * '0' + bin(i).replace('0b', '')
n = int(input())
arr = [''] * n
for i in range(n):
	st = input()
	for j in st:
		arr[i] += d[j]
arr.append('fe')
count = 1
mini = 0
store1 = []
for i in range(n):
	if arr[i] == arr[i + 1]:
		count += 1
	else:
		store1.append(count)
		count = 1
store2 = []
length = 1
for i in range(1, n):
	flag = True
	for j in range(n):
		if arr[j][i - 1] != arr[j][i]:
			flag = False
	if flag:
		length += 1
	else:
		store2.append(length)
		length = 1
store2.append(length)
ans1 = store1[0]
for x in store1:
	ans1 = math.gcd(ans1, x)
ans2 = store2[0]
for y in store2:
	ans2 = math.gcd(ans2, y)
print(math.gcd(ans1, ans2))


import sys
input = sys.stdin.readline
n = int(input())
a = [[] for i in range(n)]
for i in range(n):
	a[i] = bin(1 << n | int(input(), 16))[3:]

def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)
ans = 0
i = 0
while i < n:
	j = i + 1
	while j < n and a[i] == a[j]:
		j += 1
	ans = gcd(j - i, ans)
	k = 0
	while k < n:
		l = k + 1
		while l < n and a[i][k] == a[i][l]:
			l += 1
		ans = gcd(k - l, ans)
		k = l
	i = j
print(ans)


from math import gcd

def D():
	n = int(input())
	matrix = [0 for _ in range(n)]
	for i in range(n):
		a = bin(int(input(), 16))[2:]
		matrix[i] = '0' * (n - len(a)) + a
	ans = 0
	i = 0
	while i < n:
		j = i + 1
		while j < n and matrix[i] == matrix[j]:
			j += 1
		ans = gcd(ans, j - i)
		col = 0
		while col < n:
			l = col + 1
			while l < n and matrix[i][col] == matrix[i][l]:
				l += 1
			ans = gcd(ans, col - l)
			col = l
		i = j
	print(ans)
D()


from sys import stdin, stdout

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
n = int(stdin.readline())
a = [0] * n
for _ in range(n):
	a[_] = format(int(stdin.readline().strip(), 16), 'b').zfill(n)
r = []
t = 1
for i in range(1, n):
	if a[i] == a[i - 1]:
		t += 1
	else:
		r.append(t)
		t = 1
r.append(t)
c = []
t = 1
for i in range(1, n):
	f = 1
	for j in range(n):
		if a[j][i - 1] != a[j][i]:
			f = 0
			break
	if f == 1:
		t += 1
	else:
		c.append(t)
		t = 1
c.append(t)
ans = r[0]
for i in r + c:
	ans = gcd(ans, i)
stdout.write('%d' % ans)