#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);
    int curr = res;
    for (int dx = 1; dx < n; dx++)
    {
        for (i = 0; i < my.size() - 1; i++)
        {
            if (my[i][0] == my[i][1] && my[i][1] == my[i + 1][1] && my[i + 1][1] == my[i + 1][0])
                curr--;
        }
        hor(my, 1);
        for (i = 0; i < my.size() - 1; i++)
        {
            j = my[0].size() - 2;
            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])
                curr++;
        }
        res = max(res, curr);
        auto ret = my;
        int tc = curr;
        for (int dy = 1; dy < m; dy++)
        {
            for (i = 0; i < my[0].size() - 1; i++)
                if (my[0][i] == my[0][i + 1] && my[0][i + 1] == my[1][i + 1] && my[1][i + 1] == my[1][i])
                    tc--;
            vert(my, 1);
            for (j = 0; j < my[0].size() - 1; j++)
            {
                i = my.size() - 2;
                if (my[i][j] == my[i + 1][j] && my[i + 1][j] == my[i + 1][j + 1] && my[i + 1][j + 1] == my[i][j + 1])
                    tc++;
            }
            res = max(res, tc);
        }
        my = ret;
    }
    cout << res;
}
