#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, i, j, s = 4, x;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> x;
      if (i == 1 || j == 1 || i == n || j == m)
        if (x == 1) s = 2;
    }
  }
  cout << s;
  return 0;
}
