#include <cstdio>
#include <iostream>
using namespace std;
char s[100][100];
int ans[100],sum[100][100],n,m;
int main(){
freopen("skicourse.in","r",stdin);
freopen("skicourse.out","w",stdout);
cin >> n >> m;
int i,j;
for (i = 0; i < n; i++){
cin >> s[i];
}
for (i = 0; i < n; i++){
sum[i][0] = 1;
for (j = 1; j < m; j++){
if (s[i][j] == s[i][j-1]) sum[i][j] = sum[i][j-1]+1;
else sum[i][j] = 1;
if (sum[i][j] > ans[i]) ans[i] = sum[i][j];
}
}
m = 0x3f3f3f3f;
for (i = 0; i < n; i++)
if (ans[i] < m) m = ans[i];
if (m >= (n>>1) && m != n) m = n>>1;
printf("%d",m);
return 0;
}