#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long INF = 1e9 + 5;
const long long mod = 1e9 + 7;
const double pi = 3.1415926536;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  ;
  int n, m;
  cin >> n >> m;
  bool valid = false;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      if (x == 1 && (i == 0 || j == 0 || i == (n - 1) || j == (m - 1)))
        valid = true;
    }
  }
  if (valid)
    cout << "2\n";
  else
    cout << "4\n";
  return 0;
}
