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


using namespace std;


int n, m;
string s[1001];
char c[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];
    }
    int sum = 0;
    for (int kx = -5; kx <= 5; kx++) {
        for (int ky = -5; ky <= 5; ky++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    int i1 = (i + kx + 6 * n) % n;
                    int j1 = (j + ky + 6 * m) % m;
                    c[i1][j1] = s[i][j];
                }
            }
            int ans = 0;
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < m - 1; j++) {
                    if (c[i][j] == c[i + 1][j] && c[i][j] == c[i][j + 1] && c[i][j] == c[i + 1][j + 1]) {
                        ans++;
                    }
                }
            }
            sum = max(sum, ans);
        }
    }
    cout << sum << endl;
    return 0;
}
