#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
int n, m;
bool dfs(pair<int, int> start, vector<string>& G, vector<vector<bool> >& odw,
         set<pair<int, int> >& jed) {
  stack<pair<int, int> > S;
  S.push(start);
  odw[start.first][start.second] = true;
  bool brzeg = false;
  while (!S.empty()) {
    pair<int, int> akt = S.top();
    S.pop();
    if (akt.first == 0 || akt.first == n - 1 || akt.second == 0 ||
        akt.second == m - 1)
      brzeg = true;
    for (int i = 0; i < 8; ++i) {
      pair<int, int> nast = make_pair(akt.first + dx[i], akt.second + dy[i]);
      if (nast.first >= 0 && nast.first < n && nast.second >= 0 &&
          nast.second < m) {
        if (G[nast.first][nast.second] == '0' &&
            !odw[nast.first][nast.second]) {
          S.push(nast);
          odw[nast.first][nast.second] = true;
        }
        if (G[nast.first][nast.second] == '1') jed.insert(nast);
      }
    }
  }
  return brzeg;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int odp = 0;
  for (int i = 0; i + 1 < n; ++i)
    for (int j = 0; j + 1 < m; ++j)
      if (a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '1' &&
          a[i + 1][j + 1] == '1')
        odp = 4;
  vector<vector<bool> > odw(n, vector<bool>(m, false));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '1' || odw[i][j]) continue;
      set<pair<int, int> > S;
      if (dfs(make_pair(i, j), a, odw, S)) continue;
      int wlk = S.size();
      pair<int, int> start = *S.begin();
      pair<int, int> nast;
      int ilSa = 0;
      for (int k = 0; k < 4; ++k) {
        pair<int, int> t = make_pair(start.first + dx[k], start.second + dy[k]);
        if (S.find(t) != S.end()) {
          nast = t;
          ++ilSa;
        }
      }
      if (ilSa != 2) continue;
      S.erase(start);
      pair<int, int> akt = nast;
      bool ok = true;
      while (true) {
        ilSa = 0;
        for (int k = 0; k < 4; ++k) {
          pair<int, int> t = make_pair(akt.first + dx[k], akt.second + dy[k]);
          if (S.find(t) != S.end()) {
            nast = t;
            ++ilSa;
          }
        }
        if (ilSa > 1) {
          ok = false;
          break;
        }
        if (ilSa < 1) {
          if (S.size() > 1) ok = false;
          break;
        }
        S.erase(akt);
        akt = nast;
      }
      if (ok) odp = max(odp, wlk);
    }
  }
  cout << odp;
  return 0;
}
