#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define NAME "video"
#define FREOPEN freopen(NAME".in", "r", stdin); freopen(NAME".out", "w", stdout);

typedef long long ll;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;

const int INF = 2e9;
const ll INF64 = 9e18;
const int N = 100100;
const int M = 1010;

int n, m, ans, sum;
string s[M];

bool check(int x, int y)
{
    bool f = 1;
    if (s[x][y] != s[x - 1][y])
        f = 0;
    if (s[x][y] != s[x][y - 1])
        f = 0;
    if (s[x][y] != s[x - 1][y - 1])
        f = 0;
    return f;
}

int get()
{
    int sum = 0;
    for (int i = 1; i < n; i++)
        for (int j = 1; j < m; j++)
            if (check(i, j))
                sum++;
    return sum;
}

void up()
{
    string ss = s[0];
    forn(i, n - 1)
        s[i] = s[i + 1];
    s[n - 1] = ss;
}

void right()
{
    forn(i, n)
    {
        char c = s[i][0];
        forn(j, m - 1)
            s[i][j] = s[i][j + 1];
        s[i][m - 1] = c;
    }
}



int main()
{
    FREOPEN;
    cin >> n >> m;
    forn(i, n)
        cin >> s[i];

    ans = get();

    forn(i, 3)
    {
        up();
        forn(j, 3)
        {
            right();
            ans = max(ans, get());
        }
    }

    cout << ans;
    return 0;
}



/*

2 4
1221
1221




3 2
22
22
22

3 3
111
121
111



*/




