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