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