#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, mat[51][51], rasp = 0;
  ;
  cin >> a >> b;
  for (int k = 1; k <= a; k++)
    for (int j = 1; j <= b; j++) cin >> mat[k][j];
  for (int k = 2; k <= b; k++)
    if (mat[1][k] == 1 || mat[a][k] == 1) rasp = 2;
  for (int k = 2; k <= a; k++)
    if (mat[k][1] == 1 || mat[k][b] == 1) rasp = 2;
  if (rasp == 0)
    cout << "4";
  else
    cout << rasp;
  return 0;
}
