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