#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int arra[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &arra[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arra[i][j] and
          (i + 1 == 1 or j + 1 == 1 or i + 1 == n or j + 1 == m)) {
        cout << 2 << endl;
        return 0;
      }
    }
  }
  cout << 4 << endl;
  return 0;
}
