#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, p = 0;
  cin >> n >> m;
  int arr[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 1) {
        if (i * j == 0 || i == (n - 1) || j == (m - 1)) p++;
      }
    }
  }
  cout << ((p > 0) ? "2" : "4");
}
