#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, a[100][100];
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (i = 0; i < n; i++) {
    if (a[i][0] == 1 || a[i][m - 1] == 1) {
      cout << 2 << endl;
      return 0;
    }
  }
  for (j = 0; j < m; j++) {
    if (a[0][j] == 1 || a[n - 1][j] == 1) {
      cout << 2 << endl;
      return 0;
    }
  }
  cout << 4 << endl;
  return 0;
}
