#include <bits/stdc++.h>

using namespace std;

int a[1001][1001];
int b[1001][1001];
int sv[1001];
int sh[1001];

int main()
{
    #ifndef DEBUG
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    #else
    freopen("input.txt", "r", stdin);
    #endif // DEBUG
    int n, m;
    cin >> n >> m;
    string s;
    for (int i = 0; i < n; i++){
        cin >> s;
        for (int j = 0; j < m; j++){
            a[i][j] = s[j] - '1';
        }
    }
    int sum = 0;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            b[i][j] = (a[i][j] == a[(i + 1) % n][j] && a[i][j] == a[(i + 1) % n][(j + 1) % m] && a[i][j] == a[i][(j + 1) % m]);
            sum += b[i][j];
            sv[i] += b[i][j];
            sh[j] += b[i][j];
        }
    }
    int mx = 0;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            int cnt = sum - sv[(i+n-1)%n] - sh[(j+m-1)%m] + b[(i+n-1)%n][(j+m-1)%m];
            mx = max(cnt, mx);
        }
    }
    cout << mx;
    return 0;
}
