#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
int n, m;
int n_blocks(vector<vector<char> > a)
{
	int i, j, num = 0;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			if (a[i][j] != '-1' && a[i][j] == a[i][j + 1] && a[i][j] == a[i + 1][j] && a[i][j] == a[i + 1][j + 1]) num++;
		}
	}
	return num;
}
vector<vector<char> > a, b;
int main()
{
	ifstream in;
	ofstream out;
	in.open("video.in");
	out.open("video.out");
	int ans = 0, i, j, k;
	in >> n >> m;
	a.assign(n + 1, vector<char>(m + 1, -1));
	b.assign(n + 1, vector<char>(m + 1));
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			in >> a[i][j];
		}
	}
	ans = n_blocks(a);
	for (i = 1; i <= 2; i++) //R, D
	{
		b = a;
		if (i == 1)
		{
			for (j = 0; j < n; j++)
			{
				b[j][m] = b[j][0];
				b[j][0] = -1;
			}
		}
		if (i == 2)
		{
			b[n] = b[0];
			for (j = 0; j < m; j++) b[0][j] = -1;
		}
		ans = max(ans, n_blocks(b));
	}
	b = a;
	for (j = 0; j < n; j++)  //RD
	{
		b[j][m] = b[j][0];
		b[j][0] = -1;
	}
	b[n] = b[0];
	for (j = 0; j < m; j++) b[0][j] = -1;
	ans = max(ans, n_blocks(b));
	out << ans;
	in.close();
	out.close();
    
}