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