#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:65777216")
using namespace std;
char s[1111][1111];
int n, m;
int best;
int now;
int v[1111][1111];
vector<pair<int, int> > c;
bool u[1111][1111];
void dfs(int x, int y) {
  if (u[x][y]) return;
  u[x][y] = true;
  for (int dx(-1); dx < (2); dx++)
    for (int dy(-1); dy < (2); dy++) {
      int nx = dx + x;
      int ny = dy + y;
      if (nx >= 0 && nx < n && ny >= 0 && ny < m)
        if (s[nx][ny] == '0')
          dfs(nx, ny);
        else
          c.push_back(pair<int, int>(nx, ny));
      else
        c.push_back(pair<int, int>(-1, -1));
    }
}
int startx, starty;
int dfs2(int x, int y, int prevx = -1, int prevy = -1) {
  if (x == startx && y == starty && prevx != -1) return 0;
  int res = 1;
  for (int dx(-1); dx < (2); dx++)
    for (int dy(-1); dy < (2); dy++)
      if (abs(dx) + abs(dy) == 1) {
        int nx = x + dx;
        int ny = y + dy;
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && v[nx][ny] == v[x][y])
          if (nx != prevx || ny != prevy) {
            res += dfs2(nx, ny, x, y);
            if (prevx == -1) return res;
          }
      }
  return res;
}
int main() {
  cin >> n >> m;
  for (int i(0); i < (n); i++) scanf(" %s", s[i]);
  for (int i(1); i < (n); i++)
    for (int j(1); j < (m); j++)
      if (s[i][j] == '1' && s[i][j - 1] == '1' && s[i - 1][j - 1] == '1' &&
          s[i - 1][j] == '1')
        best = 4;
  for (int i(0); i < (n); i++)
    for (int j(0); j < (m); j++)
      if (s[i][j] == '0' && !u[i][j]) {
        c.clear();
        dfs(i, j);
        now++;
        bool good = 1;
        int num = 0;
        for (int k(0); k < (c.size()); k++)
          if (c[k].first == -1) {
            good = 0;
            break;
          } else if (v[c[k].first][c[k].second] != now)
            v[c[k].first][c[k].second] = now, num++;
        for (int k(0); k < (c.size()); k++) {
          int num = 0;
          for (int dx(-1); dx < (2); dx++)
            for (int dy(-1); dy < (2); dy++)
              if (abs(dx) + abs(dy) == 1) {
                int nx = c[k].first + dx;
                int ny = c[k].second + dy;
                if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] == '1')
                  if (v[nx][ny] == now) num++;
              }
          if (num != 2) {
            good = 0;
            break;
          }
        }
        if (good && num > 4) {
          startx = c[0].first;
          starty = c[0].second;
          if (dfs2(c[0].first, c[0].second) == num) best = max(best, num);
        }
      }
  cout << best << endl;
  return 0;
}
