#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const double eps = 1e-8;
const double PI = acos(-1);
const long long mod = 1e9 + 7;
const int inf = 1 << 30;
const int maxn = 1e3 + 5;
void err(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
int n, m;
string g[maxn];
int vs[maxn][maxn];
int vsId;
map<int, set<int>> ms;
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
inline bool val(int i, int j) { return (i > 0 && j > 0 && i <= n && j <= m); }
void rec(int i, int j) {
  ms[i].insert(j);
  vs[i][j] = vsId;
  for (int di = -1; di <= 1; ++di) {
    for (int dj = -1; dj <= 1; ++dj) {
      if (di == 0 && dj == 0) continue;
      int ni = i + di, nj = j + dj;
      if (val(ni, nj) && !vs[ni][nj] && g[ni][nj] == '0') {
        rec(ni, nj);
      }
    }
  }
}
int ch() {
  set<pair<int, int>> ones;
  for (auto it : ms) {
    auto s = it.second;
    int i = it.first;
    auto fi = *s.begin(), en = *(s.rbegin());
    for (auto j : s) {
      for (int di = -1; di <= 1; ++di) {
        for (int dj = -1; dj <= 1; ++dj) {
          if (di == 0 && dj == 0) continue;
          int ni = i + di, nj = j + dj;
          if (g[ni][nj] == '0') {
            if (vs[ni][nj] != vsId) {
              cerr << "["
                   << "ni, nj, vs[ni][nj], vsId"
                   << "] = [";
              _print(ni, nj, vs[ni][nj], vsId);
              return 0;
            }
          } else if (g[ni][nj] == '1') {
            ones.insert({ni, nj});
          } else
            return 0;
        }
      }
    }
  }
  if (ones.empty()) return 0;
  queue<pair<int, int>> q;
  map<pair<int, int>, int> vsq;
  q.push({ones.begin()->first, ones.begin()->second});
  vsq[{ones.begin()->first, ones.begin()->second}] = 1;
  int cn = 1;
  while (!q.empty()) {
    int i = q.front().first, j = q.front().second;
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int ni = i + di[k], nj = j + dj[k];
      if (ones.find({ni, nj}) != ones.end() &&
          vsq.find({ni, nj}) == vsq.end()) {
        vsq[{ni, nj}] = 1;
        ++cn;
        q.push({ni, nj});
      }
    }
  }
  if (cn != ones.size()) return 0;
  for (auto it : ones) {
    int i = it.first, j = it.second;
    int cn = 0;
    for (int k = 0; k < 4; ++k) {
      int ni = i + di[k], nj = j + dj[k];
      cn += (ones.find({ni, nj}) != ones.end());
    }
    if (cn != 2) return 0;
  }
  return ones.size();
}
void solve() {
  cin >> n >> m;
  g[0] = string(m + 2, '2');
  for (int i = 1; i <= n; ++i) {
    cin >> g[i];
    g[i] = "2" + g[i] + "2";
  }
  g[n + 1] = string(m + 2, '2');
  int ret = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (!vs[i][j] && g[i][j] == '0') {
        ms = map<int, set<int>>();
        vsId++;
        rec(i, j);
        ret = max(ret, ch());
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (g[i][j] == '1' && g[i + 1][j] == '1' && g[i][j + 1] == '1' &&
          g[i + 1][j + 1] == '1')
        ret = max(ret, 4);
    }
  }
  cout << ret;
}
int main() {
  int tc = 1;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (int tt = 0; tt < (int)(tc); ++tt) {
    solve();
  }
}
