#include <bits/stdc++.h>
using namespace std;
int read() {
  int num = 0;
  int flg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') {
      flg = -1;
    }
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 1) + (num << 3) + (c ^ 48);
    c = getchar();
  }
  return num * flg;
}
const int maxn = 2e3 + 3;
const int direx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int direy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int DIREX[4] = {-1, 0, 0, 1};
const int DIREY[4] = {0, -1, 1, 0};
int n;
int m;
int v[maxn][maxn];
int col = 1;
char s[maxn];
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));
  v[x][y] = -1;
  bool success = true;
  for (int cur = 0; cur < q.size(); cur++) {
    x = q[cur].first;
    y = q[cur].second;
    for (register int i = 0; i < 8; i++) {
      int nx = x + direx[i];
      int ny = y + direy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        if (v[nx][ny] == 0) {
          v[nx][ny] = -1;
          q.push_back(make_pair(nx, ny));
        }
        if (v[nx][ny] > 0 && v[nx][ny] != col) {
          bord = make_pair(nx, ny);
          v[nx][ny] = col;
          k++;
        }
      } else {
        success = false;
      }
    }
  }
  return success;
}
int c[maxn][maxn];
int col2 = 0;
bool bfs2(int x, int y) {
  vector<pair<int, int> > q;
  q.push_back(make_pair(x, y));
  c[x][y] = col2;
  for (register int cur = 0; cur < q.size(); cur++) {
    x = q[cur].first;
    y = q[cur].second;
    int p = 0;
    for (register int i = 0; i < 4; i++) {
      int nx = x + DIREX[i];
      int ny = y + DIREY[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        if (v[nx][ny] == col) {
          p++;
          if (c[nx][ny] < col2) {
            c[nx][ny] = col2;
            q.push_back(make_pair(nx, ny));
          }
        }
      }
    }
    if (p != 2) {
      return false;
    }
  }
  return k == q.size();
}
int main() {
  n = read();
  m = read();
  for (register int i = 0; i < n; i++) {
    scanf("%s", s);
    for (register int j = 0; j < m; j++) {
      v[i][j] = s[j] - '0';
    }
  }
  int ans = 0;
  for (register int i = 0; i < n; i++) {
    for (register int j = 0; j < m; j++) {
      if (!v[i][j]) {
        col++;
        k = 0;
        bord = make_pair(-1, -1);
        if (bfs(i, j)) {
          col2++;
          if (bfs2(bord.first, bord.second)) {
            ans = max(ans, k);
          }
        }
      }
    }
  }
  if (ans > 4) {
    return 0 & printf("%d", ans);
  }
  for (register int i = 0; i < n - 1; i++) {
    for (register int j = 0; j < m - 1; j++) {
      if (v[i][j] > 0 && v[i + 1][j] > 0 && v[i][j + 1] > 0 &&
          v[i + 1][j + 1] > 0) {
        ans = max(ans, 4);
      }
    }
  }
  return 0 & printf("%d", ans);
}
