#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;
    for(int l = 0; l < n; l++) {
        for(int k = 0; k < m; k++) {
            for(int i_ = 0; i_ < n - 1; i_++) {
                for(int j_ = 0; j_ < m - 1; j_++) {
                    int i = (i_ + l) % n;
                    int j = (j_ + k) % 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;
}
