#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>

using namespace std;

vector<string> v;
const int maxn = 1005;
int a[maxn][maxn];

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    v.resize(n * 2);
    for (int i = 0; i < n; ++i) {
        string s, s1;
        cin >> s;
        s1 = s + s;
        v[i] = s1;
        v[i + n] = s1;
    }
    /*for (int i = 0; i < n * 2; ++i) {
        cout << v[i] << endl;
    }*/
    for (int i = 1; i < n * 2; ++i) {
        for (int j = 1; j < m * 2; ++j) {
            a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
            if (v[i][j] == v[i][j - 1] && v[i][j] == v[i - 1][j - 1] && v[i][j] == v[i - 1][j]) {
                ++a[i][j];
                //cout << i << " " << j << endl;
            }
            //cout << a[i][j] << " ";
        }
        //cout << endl;
    }
    int res = 0;
    for (int i = n - 1; i < n * 2; ++i) {
        for (int j = m - 1; j < m * 2; ++j) {
            int pres = a[i][j] + a[i - n + 1][j - m + 1] - a[i][j - m + 1] - a[i - n + 1][j];
            res = max(res, pres);
        }
    }
    cout << res << endl;
    return 0;
}
