#include <bits/stdc++.h>

using namespace std;

ifstream inp("video.in");
ofstream outp("video.out");

int main() {
    int n, m;
    bool mons[1000][1000];
    inp >> n >> m;
    for(int i = 0; i < n; i++) {
        string str;
        inp >> str;
        for(int j = 0; j < m; j++) {
            mons[i][j] = (str[j] == '2');
        }
    }
    int o = 0;
    int fo = 0;
    //dont move
    for(int i = 0; i < n - 1; i++) {
        //dont move vertically
        for(int j = 0; j < m - 1; j++) {
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    for(int i = 0; i < n - 1; i++) {
        //to down vertically
        for(int j = 1; j < m; j++) {
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    for(int i = 0; i < n - 1; i++) {
        //to up vertically
        for(int j_ = m - 1; j_ < 2 * m - 2; j_++) {
            int j = j_ % m;
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    //to right horizontally
    for(int i = 1; i < n; i++) {
        //dont move vertically
        for(int j = 0; j < m - 1; j++) {
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    for(int i = 1; i < n; i++) {
        //to down vertically
        for(int j = 1; j < m; j++) {
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    for(int i = 1; i < n; i++) {
        //to up vertically
        for(int j_ = 0; j_ < m - 1; j_++) {
            int j = (j_ + m - 1) % m;
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    //to left horizontally
    for(int i_ = n - 1; i_ < 2*n - 2; i_++) {
        int i = i_ % n;
        //dont move vertically
        for(int j = 0; j < m - 1; j++) {
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    for(int i_ = n - 1; i_ < 2*n - 2; i_++) {
        int i = i_ % n;
        //to down vertically
        for(int j = 1; j < m; j++) {
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    for(int i_ = n - 1; i_ < 2*n - 2; i_++) {
        int i = i_ % n;
        //to up vertically
        for(int j_ = m - 1; j_ < 2 * m - 2; j_++) {
            int j = j_ % m;
            if(mons[i][j] == mons[i][(j+1)%m] && mons[i][j] == mons[(i+1)%n][j] &&  mons[i][j] == mons[(i+1)%n][(j+1)%m]) {
                o++;
            }
        }
    }
        fo = max(fo, o);
        o = 0;
    outp << fo;
    return 0;
}
