#include <bits/stdc++.h>

#define mp make_pair
#define pb bush_back
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 5e5 + 7;
const int M = 5e3 + 7;
const ll INF = 1e9 + 7;

char s[M][M];
int a[M][M];

void rep(int x, int y, int n, int m){
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            s[x + i][j + y] = s[i][j];
}

void init(int n, int m){
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> s[i][j];
    rep(n, 0, n, m);
    rep(0, m, n, m);
    rep(n, m, n, m);
}

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, ans = 0;
    cin >> n >> m;
    init(n, m);
    for (int i = 1; i < n * 2; i++)
        for (int j = 1; j < m * 2; j++){
            a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
            if (s[i][j] == s[i - 1][j] && s[i][j] == s[i][j - 1] && s[i][j] == s[i - 1][j - 1])
                a[i][j]++;
        }
    ans = a[n - 1][m - 1];
    for (int i = n; i < n * 2; i++)
        for (int j = m; j < m * 2; j++)
            ans = max(ans, a[i][j] - a[i - n + 1][j] - a[i][j - m + 1] + a[i - n + 1][j - m + 1]);
    cout << ans;
    return 0;
}
