#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define fi(a, b) for (li i = a; i <= b; ++i)
#define fj(a, b) for (li j = a; j <= b; ++j)

typedef long long li;

const int MAXN = 4;

li a[MAXN][MAXN], b[MAXN][MAXN], c[MAXN][MAXN], d[MAXN][MAXN], f[MAXN][MAXN];
li n, m;

bool check(li i, li j) {
	i %= n;
	j %= m;
	if (a[i][j] != a[(i + 1) % n][j])
		return false;
	if (a[i][j] != a[i][(j + 1) % m])
		return false;
	if (a[i][j] != a[(i + 1) % n][(j + 1) %m])
		return false;
	return true;

}

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	char e;

	cin >> n >> m;

	fi(0, n - 1)
		fj(0, m - 1) {
		cin >> e;
		a[i][j] = int(e) - 48;
	}

	fi(0, n - 1)
		fj(0, m - 1)
		if (check(i, j))
			f[i][j] = 1;

	fi(0, n - 1) {
		li p = 0;
		fj(0, m - 2) {
			if (check(i, j))
				++p;
		}
		b[i][0] = p;
	}
	fi(0, n - 1) {
		fj(1, m - 1) {
			b[i][j] = b[i][j - 1];
			if (check(i, j - 1))
				b[i][j]--;
			if (check(i, j + m - 2))
				b[i][j]++;
		}
	}
	fj(0, m - 1) {
		li p = 0;
		fi(0, n - 2) {
			if (check(i, j))
				++p;
		}
		c[0][j] = p;
	}
	fj(0, m - 1) {
		fi(1, n - 1) {
			c[i][j] = c[i - 1][j];
			if (check(i - 1, j))
				c[i][j]--;
			if (check(i + n - 2, j))
				c[i][j]++;
		}
	}

	fi(0, n - 2)
		d[0][0] += b[i][0];

	li k = d[0][0];

	fi(0, n - 1) {
		if (i)
			d[i][0] = d[i - 1][0] - b[i - 1][0] + b[(i + n - 2) % n][0];
		fj(1, m - 1) {
			d[i][j] = d[i][j - 1] - c[i][j - 1] + c[i][(j + m - 2) % m];
			k = max(k, d[i][j]);
		}
	}

	cout << k;

}