#include <bits/stdc++.h>
using namespace std;
int ax[] = {-1, 0, 1, 0}, ay[] = {0, 1, 0, -1};
int n, m;
int nk = 4;
char _map[1002][1002];
int vis[1002][1002][4];
vector<pair<int, int> > seq[1002 * 1002];
int tot;
vector<pair<int, int> > now;
int add(int x, int y) {
  x += y + nk;
  return x % nk;
}
int done[1002][1002];
int ds[] = {1, 0, -1};
void findcircle(int x, int y, int k) {
  if (done[x][y]) {
    vector<pair<int, int> > _s;
    while (now[now.size() - 1] != make_pair(x, y)) {
      _s.push_back(now[now.size() - 1]);
      done[now[now.size() - 1].first][now[now.size() - 1].second] = 0;
      now.pop_back();
    }
    _s.push_back(now[now.size() - 1]);
    reverse(_s.begin(), _s.end());
    seq[tot++] = _s;
    return;
  }
  if (vis[x][y][k]) return;
  vis[x][y][k] = 1;
  done[x][y] = 1;
  now.push_back(make_pair(x, y));
  for (int i = 0; i < 3; i++) {
    int ek = add(k, ds[i]);
    int ix = x + ax[ek], iy = y + ay[ek];
    if (_map[ix][iy] == '1') {
      findcircle(ix, iy, ek);
      return;
    }
  }
  return;
}
int col[1002][1002];
pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
int where(pair<int, int> p1, pair<int, int> p2) {
  for (int k = 0; k < nk; k++)
    if (merge(p1, make_pair(ax[k], ay[k])) == p2) return k;
  return -1;
}
int used[1002][1002];
void clean(queue<pair<int, int> >& qp) {
  while (qp.size()) {
    used[qp.front().first][qp.front().second] = 0;
    qp.pop();
  }
}
queue<pair<int, int> > qp;
int bfs(int x, int y) {
  if (used[x][y]) return 1;
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  qp.push(q.front());
  used[x][y] = 1;
  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int k = 0; k < nk; k++) {
      int ix = p.first + ax[k], iy = p.second + ay[k];
      if (!used[ix][iy] and ix >= 1 and iy >= 1 and ix <= n and iy <= m) {
        if (_map[ix][iy] == '1' and !col[ix][iy]) {
          return 0;
        }
        if (_map[ix][iy] == '1') continue;
        q.push(make_pair(ix, iy));
        qp.push(make_pair(ix, iy));
        used[ix][iy] = 1;
      }
    }
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  tot = 0;
  for (int i = 1; i <= n; i++) scanf("%s", _map[i] + 1);
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      if (_map[i][j] != '0')
        for (int k = 0; k <= 0; k++)
          if (!vis[i][j][k]) {
            now.clear();
            findcircle(i, j, k);
            for (int z = 0; z < now.size(); z++)
              done[now[z].first][now[z].second] = 0;
          }
    }
  }
  int ans = 0;
  for (int ivf = 0; ivf < tot; ivf++) {
    vector<pair<int, int> > s = seq[ivf];
    int fff = 1;
    for (int j = 0; j < s.size(); j++) {
      if (s[0].second > s[j].second) fff = 0;
      if (s[0].second == s[j].second and s[0].first > s[j].first) fff = 0;
    }
    if (!fff) continue;
    for (int j = 0; j < s.size(); j++) col[s[j].first][s[j].second] = 1;
    int sx = 0, sy = 0;
    int flag = 1;
    for (int i = 0; i < s.size(); i++) {
      int j = i - 1 + (int)s.size();
      j %= (int)s.size();
      int cnt = 0;
      assert(j >= 0);
      if (!flag) break;
      for (int k = 0; k < nk; k++) {
        int ix = s[i].first + ax[k], iy = s[i].second + ay[k];
        if (!flag) break;
        if (_map[ix][iy] == '1') {
          if (col[ix][iy])
            cnt += col[ix][iy];
          else if (add(where(s[j], s[i]), 1) == k)
            flag = 0;
        } else if (_map[ix][iy] == '0') {
          if (add(where(s[j], s[i]), 1) == k) flag &= bfs(ix, iy);
        }
        if (cnt > 2) {
          flag = 0;
        }
      }
    }
    if (flag) ans = max(ans, (int)s.size());
    for (int j = 0; j < s.size(); j++) col[s[j].first][s[j].second] = 0;
    clean(qp);
  }
  cout << ans;
  return 0;
}
