#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <set>
#include <algorithm>

using namespace std;

int arr[1000][1000];

int arr_count(int n, int m)
{
    int ans = 0;

    for(int y = 0; y < n-1; y++)
    {
        for(int x = 0; x < m-1; x++)
        {
            if(arr[x][y] == arr[x+1][y] && arr[x][y] == arr[x][y+1] && arr[x][y] == arr[x+1][y+1]) ans++;
        }
    }

    return ans;
}

void move_right(int n, int m)
{
    for(int y = 0; y < n; y++)
    {
        int a = arr[m-1][y];
        for(int x = m-1; x > 0; x--) arr[x][y] = arr[x-1][y];
        arr[0][y] = a;
    }
}

void move_down(int n, int m)
{
    for(int x = 0; x < m; x++)
    {
        int a = arr[x][n-1];
        for(int y = n-1; y > 0; y--) arr[x][y] = arr[x][y-1];
        arr[x][0] = a;
    }
}

void move_left(int n, int m)
{
    for(int y = 0; y < n; y++)
    {
        int a = arr[0][y];
        for(int x = 0; x < m-1; x++) arr[x][y] = arr[x+1][y];
        arr[m-1][y] = a;
    }
}

void move_up(int n, int m)
{
    for(int x = 0; x < m; x++)
    {
        int a = arr[x][0];
        for(int y = 0; y < n-1; y++) arr[x][y] = arr[x][y+1];
        arr[x][n-1] = a;
    }
}

int delta[1000][1000];

int check(int x, int y, int m)
{
    return arr[x][y] == arr[(x+1)%m][y] && arr[x][y] == arr[x][y+1] && arr[x][y] == arr[(x+1)%m][y+1];
}

int solve_for_x(int n, int m)
{
    for(int y = 0; y < n; y++) for(int x = 0; x < m; x++) delta[x][y] = 0;

    for(int y = 0; y < n-1; y++)
    {
        int x = m-1;
        if(check(x, y, m))
        {
            while(x > 0 && check(x-1, y, m)) {delta[x][y] = 0; x--;}
            delta[x][y] = 1;
        }
        x--;

        while(x > 0)
        {
            while(x > 0 && check(x-1, y, m)) {delta[x][y] = -1; x--;}
            delta[x][y] = 0;
            x--;
        }
    }

    /*for(int y = 0; y < n; y++)
    {
        for(int x = 0; x < m; x++)
        {
            cout << good[x][y];
        }
        cout << endl;
    }*/

    int ans = 0;

    for(int sx = 1; sx < m; sx++)
    {
        int cnt = 0;
        for(int y = 0; y < n; y++)
        {
            cnt += delta[sx][y];
        }
        ans = max(ans, cnt);
    }

    return ans;
}

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    for(int y = 0; y < n; y++)
    {
        for(int x = 0; x < m; x++)
        {
            char c;
            cin >> c;
            arr[x][y] = c - '0';
        }
    }

    int ans = arr_count(n, m);
    for(int i = 0; i < n; i++)
    {
        move_up(n, m);
        ans = max(ans, arr_count(n, m) + solve_for_x(n, m));
    }

    /*int ans = arr_count(n, m);
    move_up(n, m);
    for(int k = 0; k < m; k++)
    {
        move_left(n, m);
        ans = max(ans, arr_count(n, m));
    }*/

    /*int ans = 0;

    ans = max(ans, arr_count(n, m));
    move_up(n, m);
    ans = max(ans, arr_count(n, m));
    move_down(n, m);
    move_down(n, m);
    ans = max(ans, arr_count(n, m));
    move_up(n, m);

    move_left(n, m);
    ans = max(ans, arr_count(n, m));
    move_up(n, m);
    ans = max(ans, arr_count(n, m));
    move_down(n, m);
    move_down(n, m);
    ans = max(ans, arr_count(n, m));
    move_up(n, m);

    move_right(n, m);
    move_right(n, m);
    ans = max(ans, arr_count(n, m));
    move_up(n, m);
    ans = max(ans, arr_count(n, m));
    move_down(n, m);
    move_down(n, m);
    ans = max(ans, arr_count(n, m));
    move_up(n, m);*/

    cout << ans << endl;

    return 0;
}
