#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, m, n, i, j, p = 0;
  scanf("%d%d", &m, &n);
  int A[m][n];
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      scanf("%d", &A[i][j]);
      if ((A[0][j] == 1) || (A[m - 1][j] == 1) || (A[i][0] == 1) ||
          (A[i][n - 1] == 1)) {
        p = 1;
      }
    }
  }
  if (p == 0)
    printf("4");
  else
    printf("2");
  return 0;
}
