#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < n; i++)
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;

const string NAME = "video";
const int MAXN = 1005;

char field[MAXN][MAXN];
int n, m;
bool ok[MAXN][MAXN];
int sum;
int row[MAXN];
int col[MAXN];

int main()
{
#ifndef LOCAL
	freopen((NAME + ".in").c_str(), "rt", stdin);
	freopen((NAME + ".out").c_str(), "wt", stdout);
#endif
                              
	cin >> n >> m;

	forn(i, n)
		forn(j, m)
			cin >> field[i][j];

                
        forn(i, n)
		field[i][m] = field[i][0];
		
	forn(j, m)
		field[n][j] = field[0][j];

	field[n][m] = field[0][0];

	
	forn(i, n)
		forn(j, m)
			ok[i][j] = (field[i][j] == field[i + 1][j] && field[i][j] == field[i][j + 1] && field[i][j] == field[i + 1][j + 1]);

	forn(i, n)
		forn(j, m)
		{
			row[i] += ok[i][j];
			col[j] += ok[i][j];
			sum += ok[i][j];
		}
			
	int res = 0;                 

	
	forn(i, n)
		forn(j, m)
		{
			
			res = max(res, sum - col[j] - row[i] + (int)ok[i][j]);
		}
	 cout << res;
}