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