#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 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;
    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][i] && s[i][j] == s[i][c1] && s[i][j] == s[c][c1]) {
                in[i][j] = 1;
            }
            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 << max(ans3, max(ans, max(ans1, ans2))) << endl;
    return 0;
}
