#include<bits/stdc++.h>

using namespace std;

typedef long long lint;

#define X first
#define Y second
#define F(i, l, r) for(auto i = l; i != r; i++)
#define Df(i, l, r) for(auto i = l; i != r; i--)
#define rs resize
#define asg assign
#define mk make_pair
#define ins insert
#define era erase
#define all(x) x.begin(),x.end()
#define ret return
#define cont continue
#define brk break

const string fname = "video";

int n, m;
vector<vector<int> > a;
vector<int> blc, blr, blmc;
set<pair<int, int> > coord;

char myget(){
    char c = '!';
    while(c != '1' && c != '2')
        c = getc(stdin);
    ret c;
}

int ans = 0;

int main(){
    string fin = fname + ".in";
    string fout = fname + ".out";
    freopen(fin.c_str(), "r", stdin);
    freopen(fout.c_str(), "w", stdout);
    scanf("%d%d", &n, &m);
    a.rs(n); blr.rs(n); blc.rs(m); blmc.rs(m);
    blmc.asg(m, 0);
    blr.asg(n, 0); blc.asg(m, 0);
    F(i, 0, n){
        a[i].rs(m);
        F(j, 0, m)
            a[i][j] = myget();
    }
    F(i, 1, m){
        F(j, 0, n - 1){
            if(a[j][i] == a[j + 1][i] && a[j][i] == a[j][i - 1] && a[j][i] == a[j + 1][i - 1])
                blc[i]++;
        }
    }
    F(j, 0, n - 1){
        F(i, 0, m - 1){
            if(a[j][i] == a[j][i + 1] && a[j][i] == a[j + 1][i] && a[j][i] == a[j + 1][i + 1])
                blr[j]++;
        }
    }
    int base = 0;
    F(i, 0, n - 1){
        F(j, 0, m - 1){
            if(a[i][j] == a[i][j + 1] && a[i][j] == a[i + 1][j] && a[i][j] == a[i + 1][j + 1])
                base++;
        }
    }
    int addv = 0, addh = 0, inter = 0, nav = 0;
    F(i, 0, m - 1){
        if(a[0][i] == a[0][i + 1] && a[0][i] == a[n - 1][i] && a[0][i] == a[n - 1][i + 1]){
            addv++;
            coord.insert(mk(0, i));
        }
    }
    F(i, 0, n - 1){
        if(a[i][0] == a[i + 1][0] && a[i][0] == a[i][m - 1] && a[i][0] == a[i + 1][m - 1]){
            addh++;
            if(coord.find(mk(i, 0)) != coord.end())
                inter++;
        }
    }
    if(a[n - 1][0] == a[0][0] && a[n - 1][0] == a[n - 1][m - 1] && a[n - 1][0] == a[0][m - 1])
        nav++;
    if(n == 2)
        nav = 0;
    F(i, 1, m){
        if(a[n - 1][i] == a[n - 1][i - 1] && a[n - 1][i] == a[0][i] && a[n - 1][i] == a[0][i - 1])
            blmc[i]++;
    }
    F(dv, 0, n){
        F(dh, 0, m){
            int curr = base;
            if(dv != 0)
                curr += addv;
            if(dh != 0)
                curr += addh;
            if(dv != 0){
                curr -= blr[dv - 1];
            }
            if(dh != 0){
                curr -= blc[dh];
            }
            if(dv != 0 && dh != 0){
                curr -= inter;
                curr += nav;
                curr -= blmc[dh];
            }
            //printf("For dh = %d && dv = %d curr = %d\n", dh, dv, curr);
            ans = max(ans, curr);
        }
    }
    printf("%d", ans);
    ret 0;
}
