#include <bits/stdc++.h>
using namespace std;
int arr[52][52];
int main() {
  int n, m;
  cin >> n >> m;
  int flag = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j]) {
        if (j == 0 || j == m - 1 || i == 0 || i == n - 1) flag = 1;
      }
    }
  if (flag)
    cout << 2 << endl;
  else
    cout << 4 << endl;
}
