#include <bits/stdc++.h>
int main() {
  int r, c, i, j, result = 4;
  scanf("%d%d", &r, &c);
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++) {
      int p;
      scanf("%d", &p);
      if ((p == 1) && (i == 0 || j == 0 || i == r - 1 || j == c - 1))
        result = 2;
    }
  printf("%d", result);
  return 0;
}
