#include <iostream>
#include <deque>

using namespace std;

int count(deque<deque<char>> &my)
{
    int res = 0;
    for (int i = 0; i < my.size() - 1; i++)
    {
        for (int j = 0; j < my[0].size() - 1; j++)
        {
            if (my[i][j] == my[i][j + 1] && my[i][j + 1] == my[i + 1][j + 1] && my[i + 1][j + 1] == my[i + 1][j])
                res++;
        }
    }
    return res;
}

void vert(deque<deque<char>> &my, int m)
{
    if (m == 0)
        return ;
    else if (m == 1)
    {
        auto temp = my[0];
        my.pop_front();
        my.push_back(temp);
    }
    else
    {
        auto temp = my.back();
        my.pop_back();
        my.push_front(temp);
    }
}

void hor(deque<deque<char>> &my, int m)
{
    if (m == 0)
        return ;
    else if (m == 1)
    {
        for (int i = 0; i < my.size(); i++)
        {
            auto t = my[i][0];
            my[i].pop_front();
            my[i].push_back(t);
        }
    }
    else
    {
        for (int i = 0; i < my.size(); i++)
        {
            auto t = my[i].back();
            my[i].pop_back();
            my[i].push_front(t);
        }
    }
}

int main() {
    int n, m;
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> n >> m;
    deque<deque<char>> my(n, deque<char>(m));
    int i, j;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> my[i][j];
    }
    int res = count(my);
    for (int i = -1; i <= 1; i++)
    {
        for (int j = -1; j <= 1; j++)
        {
            vert(my, i);
            hor(my, j);
            res = max(res, count(my));
            vert(my, -i);
            hor(my, -j);
        }
    }
    cout << res;
}
