#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 get_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;
}

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 delta[1000][1000];

void build_delta(int n, int m)
{
    for(int y = 0; y < n-1; y++)
    {
        for(int x = 1; x < m; x++)
        {
            int a1 = 0, a2 = 0;
            if(check(x-1, y, m)) a1 = 1;
            if(check(x, y, m)) a2 = 1;
            delta[x][y] = a2-a1;
        }
    }

    for(int y = 0; y < n-1; y++)
    {
        for(int x = m-2; x >= 0; x--) delta[x][y] += delta[x+1][y];
    }
}


void move_up(int n, int m)
{
    for(int x = 0; x < m-1; x++)
    {
        if(check(x, 0, m)) arr_count--;
    }

    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;
    }

    for(int x = 0; x < m; x++)
    {
        for(int y = 0; y < n-2; y++) delta[x][y] = delta[x][y+1];
    }

    for(int x = 1; x < m; x++)
    {
        int a1 = 0, a2 = 0;
        if(check(x-1, n-2, m)) a1 = 1;
        if(check(x, n-2, m)) a2 = 1;
        delta[x][n-2] = a2-a1;
    }

    for(int x = 0; x < m-1; x++)
    {
        if(check(x, n-2, m)) arr_count++;
    }
}

int solve_for_x(int n, int m)
{
    build_delta(n, m);
    int ans = 0;
    for(int x = 1; x < m; x++)
    {
        int s = 0;
        for(int y = 0; y < n-1; y++) s += delta[x][y];
        ans = max(ans, s);
    }
    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';
        }
    }
    build_delta(n, m);

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

    cout << ans << endl;

    return 0;
}
