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