#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  long long int arr[n][m];
  vector<pair<long long int, long long int>> pos;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      if (arr[i][j] == 1) {
        pos.push_back(make_pair(i, j));
      }
    }
  }
  long long int flag = 0;
  for (auto x : pos) {
    if (x.first == 0 || x.first == n - 1) {
      flag = 1;
      break;
    } else if (x.second == 0 || x.second == m - 1) {
      flag = 1;
      break;
    }
  }
  if (flag == 1) {
    cout << "2" << endl;
  } else {
    cout << "4" << endl;
  }
}
