include

include

using namespace std;

typedef unsigned long long ull;
unordered_map <ull,bool> mp;
ull hash2[505][505],p1=469762049,p2=998244353,pow1[505],pow2[505];
int n,m;
inline ull get_hash(int x,int y,int x2,int y2)
{
return hash2[x2][y2]-hash2[x-1][y2]pow1[x2-x+1]-hash2[x2][y-1]pow2[y2-y+1]+hash2[x-1][y-1]pow1[x2-x+1]pow2[y2-y+1];
}
bool ck(int x)
{
for(int i=1;i+x-1<=n;i++)
for(int j=1;j+x-1<=m;j++)
{
ull tmp=get_hash(i,j,i+x-1,j+x-1);
if(mp[tmp]) return true;
mp[tmp]=true;
}
return false;
}
inline int min(int x,int y)
{
return x<y?x:y;
}
inline int max(int x,int y)
{
return x>y?x:y;
}
char s[505][505];
int main()
{
scanf("%d%d",&n,&m);
pow1[0]=pow2[0]=1;
for(int i=1;i<=max(n,m);i++)
{
pow1[i]=pow1[i-1]p1;
pow2[i]=pow2[i-1]
p2;
}
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
hash2[i][j]=hash2[i][j-1]p2+hash2[i-1][j]p1-hash2[i-1][j-1]p1p2+s[i][j];
}
int l=1,r=min(n,m);
while(l<r-1)
{
int mid=(l+r)>>1;
if(ck(mid)) l=mid;
else r=mid;
}
printf("%d\n",l);
}