#include <bits/stdc++.h>
using namespace std;
bool table[55][55];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> table[i][j];
  bool onEdge = false;
  for (int i = 2; i < n; i++)
    if (table[i][1] || table[i][m]) {
      onEdge = true;
      goto skip;
    }
  for (int j = 2; j < m; j++)
    if (table[1][j] || table[n][j]) {
      onEdge = true;
      goto skip;
    }
skip:
  cout << (onEdge ? 2 : 4) << endl;
  return 0;
}
