#include <bits/stdc++.h>
using namespace std;
int n, m;
int cool[1001][1001];
int k = 2;
int cmax = 0;
int c = 0;
int cy = 0;
int pi, pj;
int fl = 0;
int kl = 0;
int x[8] = {0, 1, -1, 0, 1, 1, -1, -1};
int y[8] = {1, 0, 0, -1, 1, -1, -1, 1};
bool limit(int i, int j) {
  if (i >= 0 && j >= 0 && i <= n - 1 && j <= m - 1)
    return true;
  else
    return false;
}
bool fours(int i, int j) {
  int c = 0;
  if (limit(i + 1, j) && (cool[i + 1][j] == k || cool[i + 1][j] == k - 1)) {
    c++;
    if (c > 2) return false;
  }
  if (limit(i - 1, j) && (cool[i - 1][j] == k || cool[i - 1][j] == k - 1)) {
    c++;
    if (c > 2) return false;
  }
  if (limit(i, j + 1) && (cool[i][j + 1] == k || cool[i][j + 1] == k - 1)) {
    c++;
    if (c > 2) return false;
  }
  if (limit(i, j - 1) && (cool[i][j - 1] == k || cool[i][j - 1] == k - 1)) {
    c++;
    if (c > 2) return false;
  }
  return true;
}
void ciclo(int i, int j, int proi, int proj) {
  cool[i][j] = k;
  cy++;
  if (limit(i + 1, j) && proi != 1 && cool[i + 1][j] == k - 1 &&
      fours(i + 1, j)) {
    ciclo(i + 1, j, -1, 0);
  } else {
    if (limit(i + 1, j) && proi != 1 && cool[i + 1][j] == k && i + 1 == pi &&
        j == pj && fours(i + 1, j))
      fl = 1;
    if (limit(i - 1, j) && proi != -1 && cool[i - 1][j] == k - 1 &&
        fours(i - 1, j)) {
      ciclo(i - 1, j, 1, 0);
    } else {
      if (limit(i - 1, j) && proi != -1 && cool[i - 1][j] == k && i - 1 == pi &&
          j == pj && fours(i - 1, j))
        fl = 1;
      if (limit(i, j + 1) && proj != 1 && cool[i][j + 1] == k - 1 &&
          fours(i, j + 1)) {
        ciclo(i, j + 1, 0, -1);
      } else {
        if (limit(i, j + 1) && proj != 1 && cool[i][j + 1] == k && i == pi &&
            j + 1 == pj && fours(i, j + 1))
          fl = 1;
        if (limit(i, j - 1) && proj != -1 && cool[i][j - 1] == k - 1 &&
            fours(i, j - 1)) {
          ciclo(i, j - 1, 0, 1);
        } else {
          if (limit(i, j - 1) && proj != -1 && cool[i][j - 1] == k && i == pi &&
              j - 1 == pj && fours(i, j - 1))
            fl = 1;
        }
      }
    }
  }
}
void flood_fill(int i, int j) {
  if (cool[i][j] == 0 && limit(i, j)) {
    cool[i][j] = -1;
    for (int h = 0; h < 8; h++) {
      if (limit(i + y[h], j + x[h])) {
        if (cool[i + y[h]][j + x[h]] > 0 && cool[i + y[h]][j + x[h]] != k) {
          cool[i + y[h]][j + x[h]] = k;
          pi = i + y[h];
          pj = j + x[h];
          c++;
        }
      } else
        kl = 1;
    }
    flood_fill(i + 1, j);
    flood_fill(i - 1, j);
    flood_fill(i, j + 1);
    flood_fill(i, j - 1);
    flood_fill(i - 1, j - 1);
    flood_fill(i + 1, j - 1);
    flood_fill(i - 1, j + 1);
    flood_fill(i + 1, j + 1);
  }
}
int main() {
  cin >> n >> m;
  char mat[1001];
  for (int i = 0; i < n; i++) {
    scanf("%s", mat);
    for (int j = 0; j < m; j++) {
      if (mat[j] == '0') cool[i][j] = 0;
      if (mat[j] == '1') cool[i][j] = 1;
    }
  }
  int hl = 0;
  for (int i = 0; i < n && hl == 0; i++)
    for (int j = 0; j < m && hl == 0; j++) {
      if (cool[i][j] == 1) {
        if (limit(i + 1, j) && cool[i + 1][j] == 1)
          if (limit(i, j + 1) && cool[i][j + 1] == 1)
            if (limit(i + 1, j + 1) && cool[i + 1][j + 1] == 1) {
              cmax = 4;
              hl = 1;
            }
      }
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (cool[i][j] == 0) {
        flood_fill(i, j);
        k++;
        ciclo(pi, pj, 0, 0);
        if (c == cy && fl == 1 && kl == 0 && c > cmax) cmax = c;
        c = 0;
        cy = 0;
        fl = 0;
        kl = 0;
        k++;
      }
    }
  cout << cmax;
  return 0;
}
