#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[1001][1001];
bool vis[1001][1001];
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
void dfs(int x, int y) {
  vis[x][y] = false;
  for (int i = 0; i < 4; ++i) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx >= 0 && tx < n && ty >= 0 && ty < m && vis[tx][ty] &&
        s[tx][ty] == '1')
      dfs(tx, ty);
  }
}
int get(int x, int y) {
  queue<int> qx, qy;
  vector<int> ansx, ansy;
  qx.push(x);
  qy.push(y);
  vis[x][y] = true;
  int mininx = x, mininy = y, maxinx = x, maxiny = y, minoutx = x + 1,
      minouty = y + 1, maxoutx = x - 1, maxouty = y - 1;
  while (!qx.empty()) {
    x = qx.front();
    y = qy.front();
    qx.pop();
    qy.pop();
    for (int i = 0; i < 8; ++i) {
      int tx = x + dx[i];
      int ty = y + dy[i];
      if (tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty]) {
        vis[tx][ty] = true;
        if (s[tx][ty] == '0') {
          mininx = min(tx, mininx);
          maxinx = max(tx, maxinx);
          mininy = min(ty, mininy);
          maxiny = max(ty, maxiny);
          qx.push(tx);
          qy.push(ty);
        } else {
          minoutx = min(tx, minoutx);
          maxoutx = max(tx, maxoutx);
          minouty = min(ty, minouty);
          maxouty = max(ty, maxouty);
          ansx.push_back(tx);
          ansy.push_back(ty);
        }
      }
    }
  }
  bool flag = true;
  for (int i = 0; flag && i < ansx.size(); ++i) {
    int cnt = 0;
    for (int j = 0; j < 4; ++j) {
      x = ansx[i] + dx[j];
      y = ansy[i] + dy[j];
      if (x >= 0 && x < n && y >= 0 && y < m && '1' == s[x][y] && vis[x][y])
        ++cnt;
    }
    if (cnt != 2) flag = false;
  }
  if (ansx.size() && flag) dfs(ansx[0], ansy[0]);
  for (int i = 0; i < ansx.size(); ++i)
    if (vis[ansx[i]][ansy[i]]) {
      flag = vis[ansx[i]][ansy[i]] = false;
    }
  flag = flag && (mininx > minoutx && mininy > minouty && maxinx < maxoutx &&
                  maxiny < maxouty);
  return flag ? ansx.size() : 0;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%s", s[i]);
  int ans = 0;
  for (int i = 0; i < n - 1; ++i)
    for (int j = 0; j < m - 1; ++j)
      if (ans == 0 && s[i][j] == s[i][j + 1] && s[i][j] == s[i + 1][j] &&
          s[i][j] == s[i + 1][j + 1] && s[i][j] == '1')
        ans = 4;
      else if (s[i][j] == '0' && !vis[i][j]) {
        int t = get(i, j);
        ans = ans > t ? ans : t;
      }
  cout << ans << endl;
  return 0;
}
