#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x, flag = 0;
  (scanf("%d%d", &n, &m));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      (scanf("%d", &x));
      if (i == 1 || i == n || j == 1 || j == m) {
        if (x == 1) flag = 1;
      }
    }
  }
  if (flag == 1)
    cout << "2";
  else
    cout << "4";
}
