#include <bits/stdc++.h>
using namespace std;

#define NAME "video"

int main() {
#ifndef NO_FILES
	freopen(NAME".in", "r", stdin);
	freopen(NAME".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, m; cin >> n >> m;
	vector<string> s(2 * n + 1);
	for (int i = 0; i < n; ++i) {
		cin >> s[i];
		s[i].append(s[i] + "@");
		s[n + i] = s[i];
	}
	s[2 * n].assign(2 * n + 1, '@');

	auto is_good = [&s](int i, int j) {
		return s[i][j] == s[i + 1][j] and s[i][j] == s[i][j + 1] and s[i][j] == s[i + 1][j + 1];
	};

	int y = 0;
	int ans = 0;
	int cur = 0;
	for (int i = 0; i < n - 1; ++i)
		for (int j = 0; j < m - 1; ++j)
			cur += is_good(i, j);
	ans = cur;

	int dy = 1;
	for (int x = 0; x < n; ++x) {
		for (; 0 <= y and y < m; y += dy) {
// 			cerr << x << ' ' << y << ": " << cur << endl;
			ans = max(ans, cur);
			for (int i = 0; i < n - 1; ++i) {
				if (dy == 1 and y < m - 1) {
					cur -= is_good(x + i, y);
					cur += is_good(x + i, y + n - 1);
				} else if (dy == -1 and y > 0) {
					cur -= is_good(x + i, y + n - 2);
					cur += is_good(x + i, y - 1);
				}
			}
		}
		y -= dy;
		if (x < n - 1)
			for (int j = 0; j < m - 1; ++j) {
				cur -= is_good(x, y + j);
				cur += is_good(x + n - 1, y + j);
			}
		dy = -dy;
	}

	cout << ans << endl;
}

