#include <bits/stdc++.h>
using namespace std;
bool mark[1234][1234];
char a[1234][1234];
int n, m, com;
int id[1234][1234];
vector<pair<int, int> > node;
int mX[10] = {-1, -1, -1, 0, 0, 1, 1, 1};
int mY[10] = {-1, 0, 1, -1, 1, -1, 0, 1};
inline void dfs(int x, int y) {
  mark[x][y] = true;
  if (x == n || y == m) {
    node.push_back(pair<int, int>(-1, -1));
    return;
  }
  for (int i = 0; i < 8; i++) {
    int X = x + mX[i];
    int Y = y + mY[i];
    if (a[X][Y] == '1' && id[X][Y] != com) {
      id[X][Y] = com;
      node.push_back(pair<int, int>(X, Y));
    } else if (!mark[X][Y] && a[X][Y] == '0') {
      dfs(X, Y);
    }
  }
}
inline int dfs2(int x, int y) {
  id[x][y] = -1;
  int ret = 1;
  if (id[x - 1][y] == com) ret += dfs2(x - 1, y);
  if (id[x + 1][y] == com) ret += dfs2(x + 1, y);
  if (id[x][y - 1] == com) ret += dfs2(x, y - 1);
  if (id[x][y + 1] == com) ret += dfs2(x, y + 1);
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf(" %c", &a[i][j]);
  int res = 0;
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      if (a[i][j] == '1' && a[i + 1][j] == '1' && a[i][j + 1] == '1' &&
          a[i + 1][j + 1] == '1')
        res = 4;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      if (!mark[i][j] && a[i][j] == '0') {
        com++;
        node.clear();
        dfs(i, j);
        if (node.size() < 5) continue;
        bool wr = false;
        for (int z = 0; z < (int)node.size(); z++) {
          int x = node[z].first;
          int y = node[z].second;
          if (x == -1 || y == -1) {
            wr = true;
            break;
          }
          int deg = (id[x - 1][y] == id[x][y]) + (id[x + 1][y] == id[x][y]) +
                    (id[x][y - 1] == id[x][y]) + (id[x][y + 1] == id[x][y]);
          if (deg != 2) {
            wr = true;
            break;
          }
        }
        if (!wr) {
          int p = dfs2(node[0].first, node[0].second);
          if (p == (int)node.size()) res = max(res, (int)node.size());
        }
      }
  }
  cout << res << endl;
  return 0;
}
