#include <bits/stdc++.h>
int g[51][51];
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  int cnt = 0;
  int flag = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &g[i][j]);
      if (g[i][j] == 1) {
        if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
          flag = 1;
        else
          cnt++;
      }
    }
  }
  if (flag == 1)
    printf("2\n");
  else
    printf("4\n");
  return 0;
}
