#include<bits/stdc++.h>
using namespace std;
const int maxn=190;
int dp[2][maxn][maxn][maxn];
int n,m,cur=0,lst=1;
char s[maxn][maxn];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
    for(int k=m;k>=1;k--)
    for(int i=1;i<=n;i++)
    for(int j=i;j<=n;j++)
    if(s[i][k]!=s[j][k])break;
    else 
    {
        dp[cur][k][i][j]=k;
        if(s[i][k]==s[i][k+1])dp[cur][k][i][j]=max(k,dp[cur][k+1][i][j]);
    }
    for(int ans=0;ans<=20;ans++)
    {
        if(dp[cur][1][1][n]==m){printf("%d\n",ans);return 0;}
        swap(cur,lst);
        memset(dp[cur],0,sizeof(dp[cur]));
        for(int k=1;k<=m;k++)
        for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
        {
            int tmp=dp[lst][k][i][j];
            if(tmp>0&&tmp<m)tmp=max(tmp,dp[lst][dp[lst][k][i][j]+1][i][j]);
            if(i!=j)
            {
                int l=i,r=j-1,p=i;
                while(l<=r)
                {
                    int mid=l+r>>1;
                    if(dp[lst][k][i][mid]>=dp[lst][k][mid+1][j])l=mid+1,p=mid;
                    else r=mid-1;
                }
                tmp=max(tmp,min(dp[lst][k][i][p],dp[lst][k][p+1][j]));
                p++;
                if(p<j)tmp=max(tmp,min(dp[lst][k][i][p],dp[lst][k][p+1][j]));
            }
            dp[cur][k][i][j]=tmp;
        }
    }
}