#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> vec;
bool love[5201][5201];
bool love2[5201][5201];
int a,b,c,d,e;
int ans = -1;
int gcd(int a,int b)
{
	if(b == 0)
		return a;
	else
		return (b,a%b);
}
int main() 
{
	cin.sync_with_stdio(0);
	cin.tie(0);
	cin >> a;
//	cout << __gcd(1,1) << '\n';
	for(int x = 0;x<a;x++)
	{
		string aa;
		cin >> aa;
		for(int x1 = 0;x1<a/4;x1++)
		{
		   	if(aa.at(x1) == '0')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = false;
		
			}
			
			else if(aa.at(x1) == '1')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == '2')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == '3')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == '4')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == '5')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == '6')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == '7')
		   	{
		   		love[x][x1*4] = false;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == '8')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == '9')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == 'A')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == 'B')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = false;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == 'C')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == 'D')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = false;
		   		love[x][x1*4+3] = true;
			}
			else if(aa.at(x1) == 'E')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = false;
			}
			else if(aa.at(x1) == 'F')
		   	{
		   		love[x][x1*4] = true;
		   		love[x][x1*4+1] = true;		
		   		love[x][x1*4+2] = true;
		   		love[x][x1*4+3] = true;
			}
		
			
		}
		   		
	}
	for(int x = 0;x<a;x++)
	{
		for(int x1 = 0;x1<a;x1++)
		{
			love2[x][x1] = false;
	//		cout << love[x][x1] << ' ';
		}
	//	cout << '\n';
	}
	int ans = -1;
	for(int x = 0;x<a;x++)
	{
		for(int x1 = 0;x1<a;x1++)
		{
			if(!love2[x][x1])
			{
				int og1 = x;
				int og2 = x1;
				for(int count = 1;count<5200 && x1 + count < a && x + count < a;count++)
				{
					//cout << count << '\n';
					bool con1 = true;
					for(int x2 = 0;x2<=count;x2++)
					{
						 if(love[og1+count][og2+x2] != love[og1][og2] || love[og1+x2][og2+count] != love[og1][og2])
						{
							con1 = false;
							break;
						}
					}
					if(con1)
					{
						for(int x2 = 0;x2<=count;x2++)
						{
							love2[og1+count][og2+x2] = true;
							love2[og1+x2][og2+count] = true;
						}
					}
					if(!con1 || x1 + count == a-1 || x + count == a-1)
					{
						if(ans == -1)
						{
							//cout << count << '\n';
							ans = count;
							
						}
						else
						{
							ans = __gcd(ans,count);
						//	cout << ans << '\n';
						}
						break;
					}
				}
			
			}
		}
	}
	if(ans <= 0)
		abort();
	cout << ans;

}