#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int root = 1e6 + 1;
long long int a[root], lft[root], rgt[root];
vector<long long int> v;
long long int modularExponentiation(long long int x, long long int n,
                                    long long int M) {
  long long int result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
int main() {
  long long int n, m, i, j, flag = 0, ans;
  cin >> n >> m;
  long long int a[n][m];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 1) {
        if (i == 0 || j == 0)
          flag = 1;
        else if (i == n - 1 || j == m - 1)
          flag = 1;
      }
    }
  }
  if (flag == 1)
    ans = 2;
  else
    ans = 4;
  cout << ans << endl;
}
