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