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