#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long q = 1;
  while (q--) {
    long long n, m, a;
    cin >> n >> m;
    for (long long i = 0; i < n; i++)
      for (long long j = 0; j < m; j++) {
        cin >> a;
        if (a && (i == n - 1 || j == m - 1 || i == 0 || j == 0)) {
          cout << 2;
          return 0;
        }
      }
    cout << 4;
  }
  return 0;
}
