#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m, tmp;
  cin >> n >> m;
  long long ans = 4;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> tmp;
      if (tmp == 1 && (i == n - 1 || j == m - 1 || i == 0 || j == 0)) ans = 2;
    }
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
