#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define point pair <int, int>
#define For(i, a, b) for(int i = a; i < b; ++i)
#define Out(i, a, b) for(int i = a - 1; i >= b; --i)
#define files(FileName) read(FileName); write(FileName)
#define read(FileName) freopen((FileName + ".in").c_str(), "r", stdin)
#define write(FileName) freopen((FileName + ".out").c_str(), "w", stdout)

using namespace std;

string FileName = "video";

typedef long long base;

int n, m;
char arr[1001][1001];
int sum[2001][2001];

point norm(point a) {
    if (a.x >= n) a.x -= n;
    if (a.y >= m) a.y -= m;
    return a;
}

bool check(point a) {
    point A = a;
    point B = norm({a.x + 1, a.y});
    point C = norm({a.x + 1, a.y + 1});
    point D = norm({a.x, a.y + 1});
    return arr[A.x][A.y] ==  arr[B.x][B.y] && arr[A.x][A.y] ==  arr[C.x][C.y] && arr[A.x][A.y] ==  arr[D.x][D.y];
}

int main() {
    ios::sync_with_stdio(false);
    files(FileName);
    cin >> n >> m;
    For(i, 0, n) {
        string s; cin >> s;
        For(j, 0, m) {
            arr[i][j] = s[j];
        }
    }
    For(i, 1, n + 1) {
        For(j, 1, m + 1) {
            bool ans = check({i - 1, j - 1});
            sum[i][j] = ans;
            sum[i][j + m] = ans;
            sum[i + n][j] = ans;
            sum[i + n][j + m] = ans;
        }
    }
    For(i, 1, n * 2 + 1) {
        For(j, 1, m * 2 + 1) {
            sum[i][j] += sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];
        }
    }
    int ans = 0;
    For(i, 0, n) {
        For(j, 0, m) {
            int NA = sum[i + n - 1][j + m - 1] - sum[i][j + m - 1] - sum[i + n - 1][j] + sum[i][j];
            ans = max(ans, NA);
        }
    }
    cout << ans << endl;
}

