#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> a;
int result = 0;
int code(int i, int j) {
  if (i < 0 || i >= n || j < 0 || j >= m) return -1;
  return i * 1000 + j;
}
void prnt(const set<int>& border) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j)
      if (border.find(code(i, j)) != border.end())
        printf("@");
      else
        printf("%c", a[i][j] ? a[i][j] : '.');
    printf("\n");
  }
}
pair<int, int> uncode(int c) { return make_pair(c / 1000, c % 1000); }
int step[8][2] = {{0, 1}, {1, 0},  {-1, 0},  {0, -1},
                  {1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
bool dfs(int i, int j, set<int>& border) {
  a[i][j] = 0;
  if (i == 0 || j == 0 || i == n - 1 || j == m - 1) return false;
  for (int k = 0; k < 8; ++k) {
    if (a[i + step[k][0]][j + step[k][1]] == '1')
      border.insert(code(i + step[k][0], j + step[k][1]));
  }
  for (int k = 0; k < 8; ++k) {
    if (a[i + step[k][0]][j + step[k][1]] == '0') {
      if (!dfs(i + step[k][0], j + step[k][1], border)) return false;
    }
  }
  return true;
}
void findbest(int i, int j) {
  set<int> border;
  if (dfs(i, j, border)) {
    pair<int, int> start = uncode(*(border.begin()));
    border.erase(border.begin());
    pair<int, int> cur = start;
    set<int>::iterator nxt, it;
    int cycle_len = 0;
    while (true) {
      int cnt = 0;
      ++cycle_len;
      for (int k = 0; k < 4; ++k) {
        it = border.find(code(cur.first + step[k][0], cur.second + step[k][1]));
        if (it != border.end()) {
          nxt = it;
          ++cnt;
        }
      }
      if ((cnt > 1 && cur != start) || (cur == start && cnt != 2)) {
        return;
      }
      if (cnt == 0) {
        if (abs(cur.first - start.first) + abs(cur.second - start.second) !=
            1) {
          return;
        } else
          break;
      }
      border.erase(code(cur.first, cur.second));
      cur = uncode(*nxt);
    }
    if (border.size() > 1) {
      return;
    }
    if (result < cycle_len) result = cycle_len;
  }
}
int main() {
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 1; i < n && result == 0; ++i)
    for (int j = 1; j < m; ++j)
      if (a[i][j] == '1' && a[i][j - 1] == '1' && a[i - 1][j] == '1' &&
          a[i - 1][j - 1] == '1') {
        result = 4;
        break;
      }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (a[i][j] == '0') findbest(i, j);
  cout << result << "\n";
  return 0;
}
