#include <bits/stdc++.h>

using namespace std;

int n, m;
string s[1000];
bool used[1000][1000];
int cnt = 0;
int dx[1000], dy[1000];

int main() {
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    iostream::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (s[i][j] == s[(i + 1) % n][j] && s[i][j] == s[i][(j + 1) % m] && s[i][j] == s[(i + 1) % n][(j + 1) % m]) {
                cnt++;
                dx[n - 1 - i]++;
                dy[m - 1 - j]++;
                used[i][j] = 1;
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans = max(ans, cnt - dx[i] - dy[j] + used[n - 1 - j][m - 1 - j]);
        }
    }
    cout << ans << "\n";
}
