#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define size(x) (int) x.size()
#define all(x) x.begin(), x.end()

const int maxn = 2005;
const int inf = (int) 1e9;
const long long mod = (long long) 1e9 + 7;
const long long l_inf = (long long) 4e18;

int n, m;
char a[maxn][maxn];
int sum[maxn][maxn];
int ans = 0;

inline bool good(int i, int j) {
    return a[i][j] == a[i + 1][j] && a[i][j] == a[i][j + 1] && a[i][j] == a[i + 1][j + 1];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    srand(566);

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

    while (cin >> n >> m) {
        ans = 0;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> a[i][j];
                a[i + n][j] = a[i][j + m] = a[i + n][j + m] = a[i][j];
            }
        }

        for (int i = 1; i <= 2 * n - 1; i++)
            for (int j = 1; j <= 2 * m - 1; j++)
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + good(i, j);

        for (int i = n - 1; i <= 2 * n - 1; i++)
            for (int j = m - 1; j <= 2 * m - 1; j++)
                ans = max(ans, sum[i][j] - sum[i - n + 1][j] - sum[i][j - m + 1] + sum[i - n + 1][j - m + 1]);

        cout << ans << '\n';
    }

    return 0;
}
