#include <bits/stdc++.h>
using namespace std;
void dfs(int x, int y, vector<string> &a, vector<vector<int> > &color, int c) {
  static const int dx[8]{1, 0, -1, 0, 1, 1, -1, -1};
  static const int dy[8]{0, 1, 0, -1, 1, -1, 1, -1};
  color[x][y] = c;
  int n = a.size(), m = a[0].size();
  for (int d = 0; d < (a[x][y] == '1' ? 4 : 8); ++d) {
    if (x + dx[d] >= 0 && x + dx[d] < n && y + dy[d] >= 0 && y + dy[d] < m &&
        !color[x + dx[d]][y + dy[d]] && a[x + dx[d]][y + dy[d]] == a[x][y]) {
      dfs(x + dx[d], y + dy[d], a, color, c);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<vector<int> > color(n, vector<int>(m, 0));
  int c = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (!color[i][j]) {
        dfs(i, j, a, color, c++);
      }
    }
  }
  vector<set<pair<int, int> > > cicles(c);
  vector<set<int> > colors(c);
  static const int dx[8]{1, 0, -1, 0, 1, 1, -1, -1};
  static const int dy[8]{0, 1, 0, -1, 1, -1, 1, -1};
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '0') {
        for (int d = 0; d < 8; ++d) {
          if (i + dx[d] >= 0 && i + dx[d] < n && j + dy[d] >= 0 &&
              j + dy[d] < m) {
            if (a[i + dx[d]][j + dy[d]] == '1') {
              cicles[color[i][j]].insert({i + dx[d], j + dy[d]});
              colors[color[i][j]].insert(color[i + dx[d]][j + dy[d]]);
            } else if (color[i + dx[d]][j + dy[d]] != color[i][j]) {
              colors[color[i][j]].insert(1);
              colors[color[i][j]].insert(2);
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
        cicles[color[i][j]].clear();
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < c; ++i) {
    if (colors[i].size() == 1) {
      for (auto x : cicles[i]) {
        int cnt = 0;
        for (int d = 0; d < 4; ++d) {
          cnt += cicles[i].count({x.first + dx[d], x.second + dy[d]});
        }
        if (cnt != 2) {
          cicles[i].clear();
          break;
        }
      }
      ans = max(ans, (int)cicles[i].size());
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    for (int j = 0; j < m - 1; ++j) {
      if (a[i][j] == '1' && a[i + 1][j] == '1' && a[i][j + 1] == '1' &&
          a[i + 1][j + 1] == '1') {
        ans = max(ans, 4);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
