#include <bits/stdc++.h>
using namespace std;
char D[1099][1099];
int n;
int main() {
  bool a1 = false;
  int n, m;
  cin >> n >> m;
  int a[99][99];
  int i, j;
  for (i = 1; i <= (int)n; i++) {
    for (j = 1; j <= (int)m; j++) {
      cin >> a[i][j];
    }
  }
  for (i = 1; i <= (int)n; i++) {
    for (j = 1; j <= (int)m; j++) {
      if (a[i][j] == 1 && (i == 1 || i == n || j == 1 || j == m)) {
        a1 = true;
      }
    }
  }
  puts(a1 ? "2" : "4");
  return 0;
}
