#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, n, m, x;
  bool flag = false;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &x);
      if (x == 1) {
        if (i == 0 || j == 0 || i == n - 1 || j == m - 1) flag = true;
      }
    }
  }
  if (flag)
    cout << 2;
  else
    cout << 4;
  return 0;
}
