#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, c, input;
  cin >> r >> c;
  int arr[r][c];
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      cin >> input;
      arr[i][j] = input;
    }
  }
  for (int i = 0; i < r; ++i) {
    if (arr[i][0] == 1) {
      cout << 2;
      return 0;
    }
  }
  for (int i = 0; i < r; ++i) {
    if (arr[i][c - 1] == 1) {
      cout << 2;
      return 0;
    }
  }
  for (int i = 0; i < c; ++i) {
    if (arr[0][i] == 1) {
      cout << 2;
      return 0;
    }
  }
  for (int i = 0; i < c; ++i) {
    if (arr[r - 1][i] == 1) {
      cout << 2;
      return 0;
    }
  }
  cout << 4;
  return 0;
}
