#include <bits/stdc++.h>
long long int i, j;
using namespace std;
int main() {
  int ar[60][60];
  int n, m;
  cin >> n >> m;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) cin >> ar[i][j];
  bool bol = false, gol = false;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      if (ar[i][j] == 1) {
        if ((i == 0 || i == n - 1) && (j == 0 || j == m - 1))
          bol = true;
        else if (i == 0 || i == n - 1 || j == m - 1 || j == 0)
          gol = true;
      }
    }
  if (bol == true)
    cout << 1;
  else if (gol == true)
    cout << 2;
  else
    cout << 4;
}
