#include <bits/stdc++.h>
int main() {
  int m, n, elmnt, i, ans, j;
  while (scanf("%d%d", &n, &m) == 2) {
    ans = 4;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        scanf("%d", &elmnt);
        if (elmnt == 1 && (i == 0 || j == 0 || i == n - 1 || j == m - 1)) {
          ans = 2;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
