#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  int row, com, val;
  bool say = false;
  cin >> row >> com;
  for (int i = 0; i < row; i++) {
    for (int j = 0; j < com; j++) {
      cin >> val;
      if (val == 1) {
        if (i == 0 || i == row - 1 || j == 0 || j == com - 1) say = true;
      }
    }
  }
  if (say)
    cout << 2 << endl;
  else
    cout << 4 << endl;
  return 0;
}
