#include <iostream>
#include <vector>
#include <cstdio>
#include <cassert>


using namespace std;

int main() {
#ifndef LOCAL
    assert(freopen("video.in", "r", stdin));
    assert(freopen("video.out", "w", stdout));
#endif // LOCAL
    int n, m;
    cin >> n >> m;
    vector<vector<char>> f(n + 1, vector<char>(m + 1));
    vector<vector<char>> g(n, vector<char>(m));
    vector<int> gr(n);
    vector<int> gc(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> f[i][j];
        }
        f[i][m] = f[i][0];
    }
    f[n] = f[0];
    long long int sum = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (f[i][j] == f[i + 1][j] && f[i][j] == f[i][j + 1] && f[i][j] == f[i + 1][j + 1]) {
                ++g[i][j];
                ++gr[i];
                ++gc[j];
                ++sum;
            }
        }
    }
    long long int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            ans = max(ans, sum - gr[i] - gc[j] + g[i][j]);
        }
    }
    cout << ans << endl;

    return 0;
}
