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