#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(const T a) {
  return a > 0 ? a : -a;
}
const double PI = 2.0 * acos(1.0);
const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dX[4] = {-1, 0, 0, 1};
const int dY[4] = {0, -1, 1, 0};
int n;
int m;
int a[1000][1000], col = 1;
char s[2000];
pair<int, int> bord;
int k = 0;
bool bfs(int x, int y) {
  vector<pair<int, int> > q;
  q.push_back(make_pair(x, y));
  a[x][y] = -1;
  bool kpyto = true;
  for (int cur = 0; cur < ((int)(q).size()); cur++) {
    x = q[cur].first;
    y = q[cur].second;
    for (int i = 0; i < (8); i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (0 <= xx && xx < n && 0 <= yy && yy < m) {
        if (a[xx][yy] == 0) {
          a[xx][yy] = -1;
          q.push_back(make_pair(xx, yy));
        }
        if (a[xx][yy] > 0 && a[xx][yy] != col) {
          bord = make_pair(xx, yy);
          a[xx][yy] = col;
          k++;
        }
      } else
        kpyto = false;
    }
  }
  return kpyto;
}
int w[1000][1000];
int col2 = 0;
bool bfs2(int x, int y) {
  vector<pair<int, int> > q;
  q.push_back(make_pair(x, y));
  w[x][y] = col2;
  for (int cur = 0; cur < (((int)(q).size())); cur++) {
    x = q[cur].first;
    y = q[cur].second;
    int p = 0;
    for (int i = 0; i < (4); i++) {
      int xx = x + dX[i];
      int yy = y + dY[i];
      if (0 <= xx && xx < n && 0 <= yy && yy < m) {
        if (a[xx][yy] == col) {
          p++;
          if (w[xx][yy] < col2) {
            w[xx][yy] = col2;
            q.push_back(make_pair(xx, yy));
          }
        }
      }
    }
    if (p != 2) return false;
  }
  return k == ((int)(q).size());
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < (n); i++) {
    scanf("%s", s);
    for (int j = 0; j < (m); j++) a[i][j] = s[j] - '0';
  }
  int ans = 0;
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++)
      if (a[i][j] == 0) {
        col++;
        k = 0;
        bord = make_pair(-1, -1);
        if (bfs(i, j)) {
          if (a[bord.first][bord.second] != col)
            while (1)
              ;
          col2++;
          if (bfs2(bord.first, bord.second)) {
            ans = max(ans, k);
          }
        }
      }
  }
  for (int i = 0; i < (n - 1); i++) {
    for (int j = 0; j < (m - 1); j++) {
      if (a[i][j] > 0 && a[i + 1][j] > 0 && a[i][j + 1] > 0 &&
          a[i + 1][j + 1] > 0)
        ans = max(ans, 4);
    }
  }
  cout << ans << endl;
  return 0;
}
