#include <bits/stdc++.h>
int N, M;
int main() {
  scanf("%d%d", &N, &M);
  int t = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1, x; j <= M; j++) {
      scanf("%d", &x);
      if (x && (i == 1 || j == 1 || i == N || j == M)) t = 1;
    }
  puts(t ? "2" : "4");
}
