#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int n;
int m;
int ct;
char w[1001][1001];
int was[1001][1001], was2[1001][1001], use[1001][1001];
vector<pair<int, int> > q;
int bad;
int go2(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m) return 0;
  if (was2[i][j] == ct || use[i][j] != ct) return 0;
  if (w[i][j] == '0') return 0;
  was2[i][j] = ct;
  int tmp = 1;
  for (int t = 0; t < 4; t++) {
    int ni = i + int(t == 0) - int(t == 1);
    int nj = j + int(t == 2) - int(t == 3);
    tmp += go2(ni, nj);
  }
  return tmp;
}
int go(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m) {
    bad = 1;
    return 0;
  }
  if (was[i][j]) return 0;
  if (w[i][j] == '1') {
    if (use[i][j] != ct) {
      use[i][j] = ct;
      q.push_back(make_pair(i, j));
    }
    return 0;
  }
  was[i][j] = 1;
  for (int t = 0; t < 8; t++) {
    int ni =
        i + int(t == 3 || t == 4 || t == 5) - int(t == 0 || t == 1 || t == 7);
    int nj =
        j + int(t == 1 || t == 2 || t == 3) - int(t == 5 || t == 6 || t == 7);
    go(ni, nj);
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  gets(w[0]);
  for (int i = 0; i < n; i++) gets(w[i]);
  int ans = 0;
  for (int i = 0; i + 1 < n; i++)
    for (int j = 0; j + 1 < m; j++)
      if (w[i][j] == '1' && w[i + 1][j] == '1' && w[i][j + 1] == '1' &&
          w[i + 1][j + 1] == '1')
        ans = 4;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (w[i][j] == '0' && !was[i][j]) {
        bad = 0;
        ct++;
        q.clear();
        go(i, j);
        if (!bad && ((int)(q).size()) > ans &&
            go2(q[0].first, q[0].second) == ((int)(q).size())) {
          int ok = 1;
          for (int k = 0; k < ((int)(q).size()); k++) {
            int tmp = 0;
            for (int t = 0; t < 4; t++) {
              int ni = q[k].first + int(t == 0) - int(t == 1);
              int nj = q[k].second + int(t == 2) - int(t == 3);
              if (ni >= 0 && nj >= 0 && ni < n && nj < m && use[ni][nj] == ct)
                tmp++;
            }
            if (tmp != 2) {
              ok = 0;
              break;
            }
          }
          if (ok) ans = max(ans, ((int)(q).size()));
        }
      }
  printf("%d\n", ans);
  return 0;
}
