#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, x, ans = 0;
  bool tarf = false;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> x;
      if (x == 1 && (j == m - 1 || j == 0 || i == 0 || i == n - 1)) tarf = true;
    }
  }
  if (tarf)
    cout << 2 << endl;
  else
    cout << 4 << endl;
  return 0;
}
