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