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