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