#include <iostream>
#include <vector>
#include <set>
#include <algorithm>


using namespace std;

void print(vector<int> s){
    for (int i = 0; i < s.size(); i++)
        cout << s[i] << " ";
    cout << endl;
}


int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    vector<vector<int> > s(2 * n);
    for (int i = 0; i < n; i++){
        s[i].resize(2 * m);
        for (int j = 0; j < m; j++){
            char x;
            cin >> x;
            s[i][j] = int(x - '1');
        }
        for (int j = m; j < 2 * m; j++)
            s[i][j] = s[i][j - m];
    }
    for (int i = n; i < 2 * n; i++){
        s[i].resize(2 * m);
        for (int j = 0; j < 2 * m; j++)
            s[i][j] = s[i - n][j];
    }
    int answer = 0;
    vector<vector<int> > cnt(2 * n - 1);
    for (int i = 0; i < 2 * n - 1; i++){
        cnt[i].resize(2 * m - 1);
        for (int j = 0; j < 2 * m - 1; j++){
            if (s[i][j] == s[i + 1][j] && s[i][j] == s[i][j + 1] && s[i][j] == s[i + 1][j + 1])
                cnt[i][j] = 1;
        }
    }
    vector<vector<int> > summa(2 * n);
    summa[0].resize(2 * m);
    for (int i = 0; i < 2 * m; i++)
        summa[0][i] = 0;
    for (int i = 0; i < 2 * n - 1; i++){
        summa[i + 1].resize(2 * m);
        summa[i + 1][0] = 0;
        for (int j = 0; j < 2 * m - 1; j++)
            summa[i + 1][j + 1] = summa[i + 1][j] + summa[i][j + 1] - summa[i][j] + cnt[i][j];
    }
    for (int i = n - 1; i < 2 * n - 1; i++){
        for (int j = m - 1; j < 2 * m - 1; j++){
            answer = max(answer, summa[i][j] - summa[i - n + 1][j] - summa[i][j - m + 1] + summa[i - n + 1][j - m + 1]);
        }
    }
    cout << answer;
    return 0;
}
