#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int N = 3005;

char a[N][N];
int psum[N][N];

int getPref(int x, int y) {
    if (x < 0 || y < 0) return 0;
    return psum[x][y];
}

int calc(int x, int y, int n, int m) {
    int res = 0;
    int lx = x, rx = x + n - 2;
    int ly = y, ry = y + m - 2;
    if (lx > rx || ly > ry) return 0;
    return getPref(rx, ry) - getPref(lx - 1, ry) - getPref(rx, ly - 1) + getPref(lx - 1, ly - 1);
}

int main() {
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);

    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%s", a[i]);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = m; j < 2 * m; ++j) {
            a[i][j] = a[i][j - m];
        }
    }
    for (int i = n; i < 2 * n; ++i) {
        for (int j = 0; j < 2 * m; ++j) {
            a[i][j] = a[i - n][j];
        }
    }
    for (int i = 0; i < 2 * n - 1; ++i) {
        for (int j = 0; j < 2 * m - 1; ++j) {
            int add = 0;
            if (a[i][j] == a[i][j + 1] && a[i][j] == a[i + 1][j] && a[i][j] == a[i + 1][j + 1]) add = 1;
            psum[i][j] = getPref(i - 1, j) + getPref(i, j - 1) - getPref(i - 1, j - 1) + add;
        }
    }
    int ans = 0;
    for (int x = 0; x <= n; ++x) {
        for (int y = 0; y <= m; ++y) {
            ans = max(ans, calc(x, y, n, m));
        }
    }
    cout << ans;
    return 0;
}