#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, s;
  cin >> r >> s;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= s; j++) {
      int t;
      cin >> t;
      if (t == 1) {
        if (i == 1 || i == r || j == 1 || j == s) {
          cout << "2" << endl;
          return 0;
        }
      }
    }
  }
  cout << "4" << endl;
}
