#include <bits/stdc++.h>
using namespace std;
int grid[200][200];
int n, m;
int main() {
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) scanf("%d", &grid[i][j]);
    int ans;
    bool ok = 0;
    for (int i = 1; i <= n - 2; i++)
      if (grid[i][0] == 1 || grid[i][m - 1] == 1) {
        ok = 1;
        break;
      }
    for (int i = 1; i <= m - 2; i++)
      if (grid[0][i] == 1 || grid[n - 1][i] == 1) {
        ok = 1;
        break;
      }
    if (ok) {
      puts("2");
    } else {
      puts("4");
    }
  }
  return 0;
}
