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