#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>

#define rep(i, n) for (decltype(n) i = 0; i < n; i++)
#define dep(i, n) for (decltype(n) i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (decltype(b) i = a; i <= b; i++)
#define FORD(i, a, b) for (decltype(a) i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
using namespace std;

const int maxn = 1010;
int n, m;
vector<vector<int> > b;
int ans;
void cnt()
{
	int res = 0;
	rep(i, n - 1)
	{
		rep(j, m - 1)
		{
			if (b[i][j] == b[i + 1][j] && b[i][j] == b[i][j + 1] && b[i][j] == b[i + 1][j + 1])
				res++;
		}
	}
	ans = max(ans, res);
}

void up()
{
	vector<int> tmp = b[0];
	rep(i, n - 1)
		b[i] = b[i + 1];
	b[n - 1] = tmp;
}
void up1()
{
	vector<int> tmp = b[0];
	rep(i, n - 1)
		b[i] = b[i + 1];
	b[n - 1] = tmp;
}
void down1()
{
	vector<int> tmp = b[n - 1];
	FORD(i, n - 1, 1)
		b[i] = b[i - 1];
	b[0] = tmp;
}
void right1()
{
	rep(i, n)
	{
		int tmp = b[i][m - 1];
		FORD(j, m - 1, 1)
			b[i][j] = b[i][j - 1];
		b[i][0] = tmp;
	}
}
void left1()
{
	rep(i, n)
	{
		int tmp = b[i][0];
		rep(j, m - 1)
			b[i][j] = b[i][j + 1];
		b[i][m - 1] = tmp;
	}
}
int main()
{
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	cin >> n >> m;
	b.resize(n, vector<int>(m));
	rep(i, n)
	{
		string s;
		cin >> s;
		rep(j, m)
			b[i][j] = (s[j] - '0');
	}
	cnt();
	rep(i, 20)
	{
		int x = rand();
		if (x % 4 == 0)
			left1();
		if (x % 4 == 1)
			right1();
		if (x % 4 == 2)
			up1();
		if (x % 4 == 3)
			down1();
		cnt();
	}
	cout << ans << endl;
	return 0;
}