#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long a[n][m];
  long long edg = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (i == j && i == 0 || i == 0 && j == m - 1 || i == n - 1 && j == 0 ||
          i == n - 1 && j == m - 1)
        continue;
      if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
        if (a[i][j] == 1) edg = 1;
      }
    }
  }
  if (edg)
    cout << 2;
  else
    cout << 4;
}
