#include <stdio.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <queue>
#define ll long long int;
using namespace std;
const int mod=1e9+7;

int a[1111][1111],xn[1111],xm[1111];
char q[1111];

int main(){
#pragma warning(disable:4996)
#pragma comment(linker,"/STACK:536870912")
#ifdef _DEBUG
	freopen("input.txt","rt",stdin);
	freopen("output.txt","wt",stdout);
#else
	freopen("video.in","rt",stdin);
	freopen("video.out","wt",stdout);
#endif
	int n,m,i,j,s,ans;
	scanf("%d%d",&n,&m);
	gets_s(q);
	for(i=0;i<n;i++){
		gets_s(q);
		for(j=0;j<m;j++){
			a[i][j]=q[j]-'1';
		}
	}
	s=0;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(a[i][j]==a[(i+1)%n][j]&&a[i][j]==a[i][(j+1)%m]&&a[i][j]==a[(i+1)%n][(j+1)%m]){
				s++;
			}
		}
	}
	for(i=0;i<n;i++){
		xn[i]=0;
		for(j=0;j<m;j++){
			if(a[i][j]==a[(i-1+n)%n][j]&&a[i][j]==a[i][(j+1)%m]&&a[i][j]==a[(i-1+n)%n][(j+1)%m]){
				xn[i]++;
			}
		}
	}
	for(j=0;j<m;j++){
		xm[j]=0;
		for(i=0;i<m;i++){
			if(a[i][j]==a[(i+1)%n][j]&&a[i][j]==a[i][(j-1+m)%m]&&a[i][j]==a[(i+1)%n][(j-1+m)%m]){
				xm[j]++;
			}
		}
	}
	ans=0;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			ans=max(ans,s-xn[i]-xm[j]);
		}
	}
	printf("%d",ans);

	return 0;
}