#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>


using namespace std;


int n, m;
string s[1001];
bool in[1001][1001];
int sum[1001];
int sum1[1001];

int main() {
    freopen("video.in", "r", stdin);
   freopen("video.out", "w", stdout);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    if (n < 2 || m < 2) {
        cout << 0 << endl;
        return 0;
    }
    int ans = 0;
    int ans1 = 0;
    int ans2 = 0;
    int ans3 = 0;
    int k = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int c = i + 1;
            int c1 = j + 1;
            if (c >= n) {
                c -= n;
            }
            if (c1 >= m) {
                c1 -= m;
            }
            if (s[i][j] == s[c][j] && s[i][j] == s[i][c1] && s[i][j] == s[c][c1]) {
                in[i][j] = 1;
                k++;
            }
           // cout << in[i][j];
            if (in[i][j] && i != n - 1 && j != m - 1) {
                ans++;
            }
            if (in[i][j] && i != 0 && j != m - 1) {
                ans1++;
            }
            if (in[i][j] && j != 0 && i != n - 1) {
                ans2++;
            }
            if (in[i][j] && i != 0 && j != 0) {
                ans3++;
            }
        }
        //cout << endl;
    }
    for (int i = m - 1; i >= 0; i--) {
        sum[i] = in[0][i];
        if (i != m - 1) {
            sum[i] += sum[i + 1];
        }
    }
    for (int i = n - 1; i >= 0; i--) {
        sum1[i] = in[i][0];
        if (i != n - 1) {
            sum1[i] += sum1[i + 1];
        }
    }
    int ans4 = 0;
    for (int i = 1; i < n - 1; i++) {
        for (int j = 1; j < m - 1; j++) {
            ans4 = max(ans4, k - sum1[i] - sum[j]);
        }
    }
    cout << max(ans4, max(ans3, max(ans, max(ans1, ans2)))) << endl;
    return 0;
}
