#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>

using namespace std;
int mas[1000][1000], n, m, x, y, ans, an, field[1000][1000], field1[1000][1000];
vector<pair <int, int> > good;
vector<pair <int, int> > maybe;
string s;
int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> s;
        for (int j = 0; j < m; j++){
            mas[i][j] = int(s[j]);
        }
    }
    ans = 0;
    for(int h = 0; h < n - 1; h++){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                field[i][j] = mas[(i + 1) % n][j];
            }
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                mas[i][j] = field[i][j];
            }
        }
        for(int e = 0; e < m - 1; e++){
            an = 0;
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    field1[i][j] = field[i][(j + e) % m];
                }
            }
            for(int i = 0; i < n - 1; i++){
                for(int j = 0; j < m - 1; j++){
                    if (field1[i][j] + field1[i + 1][j] + field1[i][j + 1] + field1[i + 1][j + 1] == field1[i][j] * 4)
                        an += 1;
                }
            }
            ans = max(ans, an);
        }
    }
    cout << ans;
    return 0;
}
