#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  int count = 0;
  int a[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[1][j] == 1 || a[n][j] == 1) {
        cout << "2";
        count++;
        return;
      }
    }
    if (a[i][1] == 1 || a[i][m] == 1) {
      cout << "2";
      count++;
      return;
    }
  }
  if (count == 0) cout << "4";
}
int main() {
  ios_base::sync_with_stdio(false);
  int t = 1;
  while (t--) solve();
  return 0;
}
