#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 = 1005;
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 ans = 0;

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

inline int get(int x, int y) {
    int cnt = 0;
    for (int i = x; i < x + n - 1; i++)
        for (int j = y; j < y + m - 1; j++)
            cnt += good(i, j);
    return cnt;
}

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

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

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

    ans = max(ans, get(0, 0));
    ans = max(ans, get(0, 1));
    ans = max(ans, get(1, 0));
    ans = max(ans, get(1, 1));

    cout << ans;

    return 0;
}
