#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int f = 0;
  int a;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a;
      if (a == 1) {
        if ((i == 0) || (i == n - 1)) f = 1;
        if ((j == 0) || (j == m - 1)) f = 1;
      }
    }
    if (f == 1) break;
  }
  if (f)
    cout << 2;
  else
    cout << 4;
  return 0;
}
