#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, c, result = 4;
  cin >> r >> c;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      int temp;
      cin >> temp;
      if ((temp == 1) && (i == 0 || j == 0 || i == r - 1 || j == c - 1))
        result = 2;
    }
  cout << result;
  return 0;
}
