#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, l, temp, t, n, m, ans, caseno = 0;
  string str;
  while (cin >> n >> m) {
    ans = 4;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> temp;
        if (temp) {
          if (i == 0 || j == 0 || i == n - 1 || j == m - 1) ans = 2;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
