#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, sum, tmp, ans, m, l;
int a[1000010], x[1000010], y[1000010], color[100][100],
    ci[4] = {0, 0, n - 1, n - 1}, cj[4] = {0, m - 1, 0, m - 1};
char s[1000010];
bool flag;
int dfs(int color[100][100], int x[], int y[], int l, int step) {
  int i, j, s, t, count;
  int li, lj, ri, rj, min_step = 10000000, tmp;
  count = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (color[i][j] > 0) count++;
  if (count == m * n) return (step);
  for (i = 0; i < l; i++) {
    for (j = 0; j < 4; j++) {
      li = (ci[j] < x[i] ? ci[j] : x[i]);
      lj = (ci[j] > x[i] ? ci[j] : x[i]);
      ri = (cj[j] < y[i] ? cj[j] : y[i]);
      rj = (cj[j] > y[i] ? cj[j] : y[i]);
      for (s = li; s <= ri; s++)
        for (t = lj; t <= rj; t++) color[s][t]++;
      tmp = dfs(color, x, y, l, step + 1);
      min_step = (min_step < tmp ? min_step : tmp);
      for (s = li; s <= ri; s++)
        for (t = lj; t <= rj; t++) color[s][t]--;
    }
  }
  return (min_step);
}
int main() {
  cin >> n >> m;
  l = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      cin >> tmp;
      if (tmp == 1) {
        if (i == 0 || i == n - 1) {
          cout << '2' << endl;
          return 0;
        }
        if (j == 0 || j == m - 1) {
          cout << '2' << endl;
          return 0;
        }
      }
    }
  cout << '4' << endl;
  return 0;
}
