Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).

Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. 

Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x_1, y_1), an arbitrary corner of the table (x_2, y_2) and color all cells of the table (p, q), which meet both inequations: min(x_1, x_2) ≤ p ≤ max(x_1, x_2), min(y_1, y_2) ≤ q ≤ max(y_1, y_2).

Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.


-----Input-----

The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).

Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers a_{i}1, a_{i}2, ..., a_{im}. If a_{ij} equals zero, then cell (i, j) isn't good. Otherwise a_{ij} equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.


-----Output-----

Print a single number — the minimum number of operations Simon needs to carry out his idea.


-----Examples-----
Input
3 3
0 0 0
0 1 0
0 0 0

Output
4

Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0

Output
2



-----Note-----

In the first sample, the sequence of operations can be like this: [Image]   For the first time you need to choose cell (2, 2) and corner (1, 1).  For the second time you need to choose cell (2, 2) and corner (3, 3).  For the third time you need to choose cell (2, 2) and corner (3, 1).  For the fourth time you need to choose cell (2, 2) and corner (1, 3). 

In the second sample the sequence of operations can be like this: [Image]   For the first time you need to choose cell (3, 1) and corner (4, 3).  For the second time you need to choose cell (2, 3) and corner (1, 1).

Difficulty: EASY

Solutions:
(n, m) = [int(x) for x in input().split()]
arr = []
for i in range(n):
	line = [int(x) for x in input().split()]
	arr.append(line)
for i in range(n):
	if arr[i][0] == 1 or arr[i][m - 1] == 1:
		print('2')
		quit()
for j in range(m):
	if arr[0][j] == 1 or arr[n - 1][j] == 1:
		print('2')
		quit()
print('4')


(n, m) = map(int, input().split())
for _ in range(n):
	l = input()
	if '1' in l and _ == 0:
		print(2)
		exit()
	if '1' in l and _ == n - 1:
		print(2)
		exit()
	if '1' == l[0] or '1' == l[-1]:
		print(2)
		exit()
print(4)


(n, m) = map(int, input().split())
t = []
for i in range(n):
	t.append(list(map(int, input().split())))
	if t[i][0] == 1 or t[i][-1] == 1 or (i == 0 and 1 in t[i]) or (i == n - 1 and 1 in t[i]):
		print(2)
		exit()
print(4)


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
f = False
for i in range(n):
	if a[i][0] == 1 or a[i][-1]:
		f = True
		break
if not f:
	if 1 in a[-1] or 1 in a[0]:
		f = True
if f:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
flag = False
daf = list(map(int, input().split()))
if sum(daf) > 0:
	flag = True
if n >= 2:
	for i in range(n - 2):
		daf = input()
		if daf[0] == '1' or daf[-1] == '1':
			flag = True
	daf = list(map(int, input().split()))
	if sum(daf) > 0:
		flag = True
if flag:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
data = [[0] * m] * n
for i in range(0, n):
	data[i] = list(map(int, input().split()))
flag = 1
for i in range(0, n):
	if data[i][0] == 1 or data[i][m - 1] == 1:
		flag = 0
for i in range(0, m):
	if data[0][i] == 1 or data[n - 1][i] == 1:
		flag = 0
print(2 + flag * 2)


def solve(n, m, a):
	for i in range(n):
		for j in range(m):
			if a[i][j] == 1:
				if i == 0 or j == 0 or i == n - 1 or (j == m - 1):
					return 2
	else:
		return 4
(n, m) = map(int, input().split())
a = []
for i in range(n):
	b = list(map(int, input().split()))
	a.append(b)
print(solve(n, m, a))


def color_allcells(n, m, a):
	for x in range(n):
		for y in range(m):
			if a[x][y] == '1':
				if y == 0 or y == m - 1:
					return 2
				elif x == 0 or x == n - 1:
					return 2
	return 4
(n, m) = list(map(int, input().split()))
a = []
for x in range(n):
	a.append(input().split())
print(color_allcells(n, m, a))


X = list(map(int, input().split()))
for i in range(X[0]):
	Temp = input()
	if (i == 0 or i == X[0] - 1) and '1' in Temp:
		print(2)
		exit()
	if '1' in Temp and (Temp.index('1') == 0 or Temp[::-1].index('1') == 0):
		print(2)
		exit()
print(4)


(r, c) = map(int, input().split())
a = []
for i in range(r):
	a.append(input().split())
for i in range(r):
	for j in range(c):
		if a[i][j] == '1' and (i == 0 or j == 0 or i == r - 1 or (j == c - 1)):
			print(2)
			quit()
print(4)


(a, b) = map(int, input().split())
l = [input().split() for x in range(a)]
for i in range(a):
	for j in range(b):
		if l[i][j] == '1':
			if i == 0 or j == 0 or i + 1 == a or (j + 1 == b):
				print(2)
				exit()
print(4)


(n, m) = [int(x) for x in input().split()]
l = []
for i in range(n):
	row = [int(x) for x in input().split()]
	l.append(row)
found = False
for i in range(n):
	for j in range(m):
		if l[i][j] == 1:
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				found = True
				print('2')
				break
	if found:
		break
if not found:
	print('4')


(n, m) = list(map(int, input().split()))
good_cells = []
for i in range(n):
	row = list(map(int, input().split()))
	for (j, element) in enumerate(row):
		if element == 1:
			good_cells.append((i, j))

def ismarginal(cell):
	(x, y) = cell
	if x == 0 or x == n - 1:
		return True
	elif y == 0 or y == m - 1:
		return True
	return False
condition = False
for cell in good_cells:
	if ismarginal(cell):
		condition = True
		break
if condition:
	print(2)
else:
	print(4)


try:
	(n, m) = map(int, input().split())
	f = []
	flag = 0
	for i in range(n):
		k = [int(j) for j in input().split()]
		f.append(k)
	if 1 in f[0]:
		flag = 1
	for i in range(n):
		if f[i][0] == 1:
			flag = 1
	if 1 in f[n - 1]:
		flag = 1
	for i in range(n):
		if f[i][m - 1] == 1:
			flag = 1
	if flag == 1:
		print('2')
	else:
		print('4')
except:
	pass


(n, m) = map(int, input().split())
from sys import *
ans = 4
for _ in range(n):
	a = [int(x) for x in input().split()]
	if _ == 0 or _ == n - 1:
		if a[0] or a[-1]:
			print(1)
			exit()
		else:
			for i in a[1:-1]:
				if i:
					ans = 2
	elif a[0] or a[-1]:
		ans = 2
print(ans)


def main():
	(n, m) = map(int, input().split(' '))
	good = []
	for i in range(n):
		line = list(map(int, input().split(' ')))
		for j in range(m):
			if line[j] == 1:
				good.append((i, j))
	ret = 4
	for (i, j) in good:
		if i == 0 or j == 0 or i == n - 1 or (j == m - 1):
			ret = 2
	return ret
print(main())


(n, m) = map(int, input().split())
tr = False
for i in range(n):
	l = list(map(int, input().split()))
	for j in range(m):
		if l[j] == 1:
			if not i or i == n - 1:
				tr = True
			elif not j or j == m - 1:
				tr = True
if tr:
	print(2)
else:
	print(4)


(n, m) = list(map(int, input().split()))
table = []
for i in range(n):
	rows = input().split()
	rows_num = [int(x) for x in rows]
	table.append(rows_num)
isVal = False
for i in range(m):
	if table[0][i] == 1 or table[n - 1][i] == 1:
		isVal = True
for i in range(n):
	if table[i][0] == 1 or table[i][m - 1] == 1:
		isVal = True
if isVal:
	print(2)
else:
	print(4)


(n, m) = list(map(int, input().split()))
table = []
for i in range(n):
	table.append(list(map(int, input().split())))
haveonvertical = False
haveonhorizont = False
for x in table:
	if x[0] == 1 or x[-1] == 1:
		haveonvertical = True
for y in table[0]:
	if y == 1:
		haveonhorizont = True
for y in table[-1]:
	if y == 1:
		haveonhorizont = True
if haveonhorizont or haveonvertical:
	print(2)
else:
	print(4)


(n, m) = list(map(int, input().split(' ')))
g = []
good = []
for i in range(n):
	g.append(input().split(' '))
	for j in range(m):
		if g[i][j] == '1':
			good.append((i, j))
c = 4
for i in good:
	if i[0] == 0 or i[1] == 0 or i[0] == n - 1 or (i[1] == m - 1):
		c = 2
print(c)


(n, m) = map(int, input().split())
check = 0
for i in range(n):
	s = input()
	if i == 0 or i == n - 1:
		if '1' in s:
			check = 1
			break
	elif s[0] == '1' or s[-1] == '1':
		check = 1
		break
if check:
	print(2)
else:
	print(4)


from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations
from math import gcd, floor, inf
from bisect import bisect_right, bisect_left
(n, m) = map(int, input().split())
end = False
for i in range(1, n + 1):
	s = input()
	if s[-1] == '1' or s[0] == '1':
		end = True
	if i == 1 or i == n:
		if '1' in s:
			end = True
	if end:
		print(2)
		exit()
print(4)


a = input().split()
n = int(a[0])
m = int(a[1])
g = []
for i in range(n):
	g.append(input().split())
mine = 4
for i in range(1, n - 1):
	if g[i][0] == '1' or g[i][m - 1] == '1':
		mine = 2
for i in range(1, m - 1):
	if g[0][i] == '1' or g[n - 1][i] == '1':
		mine = 2
print(mine)


def table(lst, n, m):
	for i in range(n):
		for j in range(m):
			if lst[i][j] == '1':
				if i == 0 or i + 1 == n or j == 0 or (j + 1 == m):
					return 2
	return 4
(N, M) = [int(x) for x in input().split()]
arr = [input().split() for i in range(N)]
print(table(arr, N, M))


data = input().split()
(N, M) = (int(data[0]), int(data[1]))
table = []
for i in range(N):
	table.append(input().split())
answer = 0
if any((e == '1' for e in table[0])):
	answer = 2
elif any((e == '1' for e in table[-1])):
	answer = 2
else:
	for i in range(N):
		if table[i][0] == '1':
			answer = 2
		elif table[i][-1] == '1':
			answer = 2
if answer == 0:
	print('4')
else:
	print(answer)


(n, m) = map(int, input().split())
l = []
f = 0
for i in range(n):
	l.append(input().split())
for i in range(n):
	for j in range(m):
		if l[i][j] == '1':
			if (i == 0 or i == n - 1) or (j == 0 or j == m - 1):
				f = 1
				break
	if f == 1:
		break
if f == 0:
	print(4)
else:
	print(2)


(n, m) = list(map(int, input().split()))
edge = False
matrix = []
for i in range(n):
	arr = list(map(int, input().split()))
	if arr[0] == 1 or arr[-1] == 1:
		edge = True
	matrix.append(arr)
for i in range(m):
	if matrix[0][i] == 1 or matrix[-1][i] == 1:
		edge = True
corner = False
if matrix[0][0] == 1 or matrix[0][-1] == 1 or matrix[-1][0] == 1 or (matrix[-1][-1] == 1):
	corner = True
if corner:
	print(1)
elif edge:
	print(2)
else:
	print(4)


(n, _) = map(int, input().split())
if '1' in input():
	print(2)
else:
	for _ in range(n - 2):
		s = input()
		if s[0] == '1' or s[-1] == '1':
			print(2)
			break
	else:
		print(4 - 2 * ('1' in input()))


(n, m) = [int(x) for x in input('').split()]
l = []
for i in range(n):
	ar = list(map(int, input('').split()))
	l.append(ar)
dr = 0
for i in l:
	if i[0] == 1 or i[m - 1] == 1:
		dr = 1
zipped_rows = zip(*l)
t = [list(row) for row in zipped_rows]
for i in t:
	if i[0] == 1 or i[n - 1] == 1:
		dr = 1
print([4, 2][dr])


(rows, columns) = input().split()
rows = int(rows)
columns = int(columns)
rectangle = []
bad = 0
onSide = False
for i in range(rows):
	rectangle.append(input().split())
for i in range(rows):
	for j in range(columns):
		if rectangle[i][j] == '1' and (rectangle[i][j] == rectangle[0][j] or rectangle[i][j] == rectangle[i][0] or rectangle[i][j] == rectangle[i][columns - 1] or (rectangle[i][j] == rectangle[rows - 1][j])):
			onSide = True
			break
if onSide:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(input().split())
ans = 4
for i in range(n):
	for j in range(m):
		if a[i][j] != '1':
			continue
		if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
			ans = 2
print(ans)


(n, m) = list(map(int, input().split()))
A = [list(map(int, input().split())) for i in range(n)]
count = 4
for i in range(m):
	if A[0][i] == 1 or A[-1][i] == 1:
		count = 2
for i in range(n):
	if A[i][0] == 1 or A[i][-1] == 1:
		count = 2
print(count)


(n, m) = map(int, input().split())
co = False
L = []
for i in range(n):
	col = list(map(int, input().split()))
	L.append(col)
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if L[i - 1][j - 1] == 1:
			if (i == 1 or i == n) or (j == 1 or j == m):
				co = True
				break
if co:
	print('2')
else:
	print('4')


(n, m) = input().split()
n = int(n)
m = int(m)
b = False
for i in range(n):
	f = list(map(int, input().split()))
	if f[0] == 1 or f[m - 1] == 1:
		b = True
	if i == 0 or i == n - 1:
		for j in range(m):
			if f[j]:
				b = True
if b:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
t = []
min = 10 ** 9
for i in range(n):
	a = list(map(int, input().split()))
	for j in range(m):
		if a[j] == 1:
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				if 2 < min:
					min = 2
			elif 4 < min:
				min = 4
print(min)


def main():
	(n, m) = map(int, input().split())
	a = []
	for _ in range(n):
		a.append([int(i) for i in input().split()])
	ok = 0
	ok |= sum(a[0])
	ok |= sum(a[-1])
	for row in a:
		ok |= row[0] + row[-1]
	print(2 if ok else 4)
main()


import sys

class MyReader:

	def __init__(self):
		filename = 'file.in'
		if self.isLocal():
			self.file = open(filename)
		self.str = ['']
		self.ind = 1

	def isLocal(self):
		return len(sys.argv) > 1 and sys.argv[1] == 'SCHULLZ'

	def nextString(self):
		if self.isLocal():
			return self.file.read()
		else:
			return input()

	def nextInt(self):
		return int(self.nextToken())

	def nextToken(self):
		if self.ind >= len(self.str):
			self.ind = 0
			self.str = self.nextString().split()
		self.ind += 1
		return self.str[self.ind - 1]

def Solve():
	n = -1
	try:
		n = rdr.nextInt()
	except Exception:
		return False
	m = rdr.nextInt()
	a = [[0 for x in range(m)] for y in range(n)]
	ans = 4
	for i in range(0, n):
		for j in range(0, m):
			a[i][j] = rdr.nextInt()
			if (i == 0 or i == n - 1 or j == 0 or (j == m - 1)) and a[i][j] == 1:
				ans = 2
	print(ans)
	return True
rdr = MyReader()
if not rdr.isLocal():
	Solve()
else:
	while Solve():
		fgh = 1


import sys
inf = float('inf')
(mod, MOD) = (1000000007, 998244353)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
(n, m) = get_ints()
mat = []
for i in range(n):
	mat.append(get_array())
flag = 0
for i in range(n):
	for j in range(m):
		if mat[i][j] == 1:
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				flag = 1
if flag == 1:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
table = [input().split(' ') for _ in range(n)]
for i in range(n):
	for j in range(m):
		if table[i][j] == '1' and (i == 0 or i == n - 1 or j == 0 or (j == m - 1)):
			print(2)
			exit()
print(4)


(a, b) = map(int, input().split())
r = lambda : list(map(int, input().split()))
arr = [r() for _ in range(a)]
f = False
for i in range(a):
	for j in range(b):
		if arr[i][j] == 1 and (i == 0 or i == a - 1 or j == 0 or (j == b - 1)):
			f = True
			break
print(2 if f else 4)


alpha = input().split()
n = int(alpha[0])
m = int(alpha[1])
isTwo = False
for i in range(n):
	y = input().split()
	for j in range(len(y)):
		if int(y[j]) == 1:
			if i == 0 or i == n - 1:
				isTwo = True
			if j == 0 or j == m - 1:
				isTwo = True
if isTwo:
	print(2)
else:
	print(4)


def main():
	(n, m) = map(int, input().split())
	l = list((tuple(map(int, input().split())) for _ in range(n)))
	lt = list(tuple(zip(*l)))
	print(4 - 2 * (any(l[0]) or any(l[-1]) or any(lt[0]) or any(lt[-1])))

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
cnt = 4
mini = 4
flag = 0
for i in range(n):
	a = list(map(str, input().split()))
	if i == 0 and flag == 0:
		if '1' in a:
			cnt = 2
			flag = 1
	if i == n - 1 and flag == 0:
		if '1' in a:
			cnt = 2
			flag = 1
	if a[m - 1] == '1' and flag == 0:
		cnt = 2
		flag = 1
	if a[0] == '1' and flag == 0:
		cnt = 2
		flag = 1
	elif flag == 0:
		cnt = 4
print(cnt)


(n, m) = map(int, input().split())
mat = []
for i in range(n):
	a = list(map(int, input().split()))
	mat.append(a)
flag = 0
for i in range(n):
	for j in range(m):
		if mat[i][j] == 1:
			if i == 0 or j == 0 or j == m - 1 or (i == n - 1):
				flag = 1
if flag == 1:
	print(2)
else:
	print(4)


(n, m) = list(map(int, input().split()))
a = []
g = []
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	for j in range(m):
		if a[i][j] == 1:
			g.append((i, j))
l = len(g)
two = False
for i in range(l):
	if g[i][0] == 0 or g[i][1] == 0 or g[i][0] == n - 1 or (g[i][1] == m - 1):
		two = True
		break
if two:
	print(2)
else:
	print(4)


import sys
(n, m, *l) = map(int, sys.stdin.read().split())
n = int(n)
m = int(m)
r = 4
l.reverse()
for i in range(n):
	for j in range(m):
		x = l.pop()
		if x == 1 and (i == 0 or i == n - 1 or j == 0 or (j == m - 1)):
			r = 2
print(r)


(n, m) = map(int, input().split())
flag = flag1 = False
for i in range(n):
	m = list(map(int, input().split()))
	if m[0] == 1 or m[-1] == 1:
		flag = True
		if i == 0 or i == n - 1:
			flag1 = True
	elif (i == 0 or i == n - 1) and 1 in m:
		flag = True
if flag1:
	print(1)
elif flag:
	print(2)
else:
	print(4)


def arr_2d(n):
	return [[int(x) for x in input().split()] for i in range(n)]
(n, m) = map(int, input().split())
a = arr_2d(n)
for i in range(n):
	if (i == 0 or i == n - 1) and sum(a[i]) >= 1:
		exit(print(2))
	elif a[i][0] == 1 or a[i][m - 1] == 1:
		exit(print(2))
print(4)


(n, m) = map(int, input().split())
arr = []
for i in range(n):
	arr.append(list(map(int, input().split())))
if arr[0][0] == 1 or arr[0][m - 1] == 1 or arr[n - 1][0] == 1 or (arr[n - 1][m - 1] == 1):
	print(1)
else:
	flag = 0
	for i in range(n):
		if arr[i][0] == 1:
			flag = 1
		if arr[i][m - 1] == 1:
			flag = 1
	for i in range(m):
		if arr[0][i] == 1:
			flag = 1
		if arr[n - 1][i] == 1:
			flag = 1
	if flag == 1:
		print(2)
	else:
		print(4)


(n, m) = map(int, input().split())
a = [input().split() for i in range(n)]
b = [*zip(*a)]
print([4, 2]['1' in ''.join([*a[0], *a[n - 1], *b[0], *b[m - 1]])])


(n, m) = map(int, input().split())
(x, y) = ([], [])
b = 0
for i in range(n):
	t = list(map(int, input().split()))
	for j in range(m):
		if t[j] == 1:
			y.append(j + 1)
			x.append(i + 1)
for i in y:
	if i == 1 or i == m:
		b = 2
		break
for i in x:
	if i == 1 or i == n:
		b = 2
		break
if b == 2:
	print(2)
else:
	print(4)


n = list(map(int, input().split()))
corner = False
thing = []
for row in range(n[0]):
	thing.append(list(map(int, input().split())))
	for column in range(n[1]):
		if column == 0 or column == n[1] - 1:
			if thing[row][column] == 1 or ((row == 0 or row == n[0] - 1) and 1 in thing[row]):
				corner = True
if corner:
	print(2)
else:
	print(4)


import sys, random, math
(n, m) = map(int, input().split())
uh = []
for i in range(n):
	a = list(map(int, input().split()))
	uh.append(a)
if uh[0][0] == 1 or uh[0][m - 1] == 1 or uh[n - 1][0] == 1 or (uh[n - 1][m - 1] == 1):
	print(1)
	sys.exit()
else:
	for i in range(n):
		if uh[i][0] == 1 or uh[i][m - 1] == 1:
			print(2)
			sys.exit()
	for i in range(m):
		if uh[0][i] == 1 or uh[n - 1][i] == 1:
			print(2)
			sys.exit()
print(4)


import sys
a = 0
z = list(map(int, input().split()))
for i in range(z[0]):
	v = list(map(int, input().split()))
	for j in range(v.count(1)):
		if v.index(1) != 0 and v.index(1) != z[1] - 1:
			a += 2
		if i != 0 and i != z[0] - 1:
			a += 2
		if a == 2:
			print(2)
			sys.exit(0)
		a = 0
		v[v.index(1)] = 0
print(4)


(n, m) = map(int, input().split())
L = []
C = []
for i in range(n):
	L.append(list(map(int, input().split())))
case = False
for i in range(n):
	if L[i][0] == 1 or L[i][m - 1] == 1:
		case = True
for i in range(m):
	if L[0][i] == 1 or L[n - 1][i] == 1:
		case = True
if case:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
border = False
for i in range(n):
	a = list(map(int, input().split()))
	if (i == 0 or i == n - 1) and 1 in a or a[0] == 1 or a[-1] == 1:
		border = True
if border:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
l = []
for _ in range(n):
	l.append(list(map(int, input().split())))
ans = 0
for i in range(n):
	for j in range(m):
		if l[i][j] == 1 and (j == 0 or i == 0 or j == m - 1 or (i == n - 1)):
			ans = 2
			print(ans)
			exit(0)
		elif l[i][j] == 1 and (j != 0 and i != 0):
			ans = 4
if ans == 0:
	ans = 4
print(ans)


(n, m) = map(int, input().split())
arr = [input().split() for i in range(n)]
for i in range(n):
	for j in range(m):
		if arr[i][j] == '1':
			if i == 0 or i + 1 == n or j == 0 or (j + 1 == m):
				print(2)
				exit()
print(4)


(n, m) = map(int, input().split())
mini = 10
for k in range(n):
	s = input().split()
	for j in range(m):
		if s[j] == '1':
			if j != 0 and k != 0 and (j != m - 1) and (k != n - 1):
				mini = min(4, mini)
			else:
				mini = min(2, mini)
print(mini)


a = 0
lis = list(map(int, input().split()))
n = lis[0]
m = lis[1]
flag = 0
for i in range(n):
	v = list(map(int, input().split()))
	for j in range(v.count(1)):
		if v.index(1) != 0 and v.index(1) != m - 1:
			a += 2
		if i != 0 and i != n - 1:
			a += 2
		if a == 2:
			print(2)
			flag = 1
			break
		a = 0
		v[v.index(1)] = 0
	if flag == 1:
		break
if flag == 0:
	print(4)


(n, m) = map(int, input().split())
arr = []
for i in range(n):
	arr.append(input().split())
for i in range(n):
	for j in range(m):
		if arr[i][j] == '1' and (i == 0 or j == 0 or i == n - 1 or (j == m - 1)):
			print(2)
			quit()
print(4)


(r, c) = [int(x) for x in input().split()]
t = r
count = 0
x = [int(x) for x in input().split()]
for i in range(c):
	if x[i] == 1:
		count += 1
for i in range(1, t - 1):
	x = [int(x) for x in input().split()]
	if x[0] == 1 or x[c - 1] == 1:
		count += 1
x = [int(x) for x in input().split()]
for i in range(c):
	if x[i] == 1:
		count += 1
if count > 0:
	print(2)
else:
	print(4)


class CodeforcesTask359ASolution:

	def __init__(self):
		self.result = ''
		self.n_m = []
		self.board = []

	def read_input(self):
		self.n_m = [int(x) for x in input().split(' ')]
		for x in range(self.n_m[0]):
			self.board.append([int(y) for y in input().split(' ')])

	def process_task(self):
		if 1 in self.board[0] or 1 in self.board[-1]:
			self.result = '2'
		elif 1 in [x[0] for x in self.board] or 1 in [x[-1] for x in self.board]:
			self.result = '2'
		else:
			self.result = '4'

	def get_result(self):
		return self.result
Solution = CodeforcesTask359ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


dim = [int(x) for x in input().split()]
flag = 0
a = []
for i in range(dim[0]):
	a = [int(y) for y in input().split()]
	if i == 0 or i == dim[0] - 1:
		for j in range(1, dim[1] - 1):
			if a[j] == 1:
				flag = 1
	if a[0] == 1 or a[dim[1] - 1] == 1:
		flag = 1
if flag == 1:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
L = [list(map(int, input().split())) for i in range(n)]
k = 0
for i in range(1, n - 1):
	if L[i][0] == 1 or L[i][-1] == 1:
		k += 1
for j in range(1, m - 1):
	if L[0][j] == 1 or L[-1][j] == 1:
		k += 1
if k > 0:
	print(2)
else:
	print(4)


(rows, columns) = [int(x) for x in input().split()]
table = []
count = 0
for i in range(rows):
	table.append([int(x) for x in input().split()])
for test in table[0]:
	if test == 1:
		count += 1
for test in table[-1]:
	if test == 1:
		count += 1
for i in range(rows):
	if table[i][0] == 1:
		count += 1
	elif table[i][-1] == 1:
		count += 1
if count >= 1:
	print('2')
else:
	print('4')


(n, m) = map(int, input().split())
for i in range(n):
	lis = list(map(int, input().split()))
	if 0 < i < n - 1:
		if lis[0] == 1 or lis[m - 1] == 1:
			print(2)
			exit()
	elif 1 in lis:
		print(2)
		exit()
print(4)


(n, m) = map(int, input().split())
b = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
	if 1 in b[0] or 1 in b[n - 1]:
		print(2)
		break
	elif b[i][0] == 1 or b[i][m - 1] == 1:
		print(2)
		break
	elif i == n - 1:
		print(4)


(n, m) = map(int, input().split())
mat = []
for i in range(n):
	l = list(map(int, input().split()))
	mat.append(l)
flag = False
for i in range(n):
	for j in range(m):
		if flag == False and mat[i][j] == 1:
			if i == 0 or j == 0 or i == n - 1 or (j == m - 1):
				print('2')
				flag = True
				break
if flag == False:
	print('4')


(n, m) = [int(i) for i in input().split()]
c = 4
for i in range(n):
	x = [int(j) for j in input().split()]
	if i == 0 and any(x):
		c = 2
	elif i == n - 1 and any(x):
		c = 2
	elif x[0] or x[-1]:
		c = 2
print(c)


(n, m) = map(int, input().split())
(x, y) = (set(), set())
for i in range(n):
	l = list(map(str, input().split()))
	for j in range(m):
		if l[j] == '1':
			x.add(i)
			y.add(j)
ans = 4
for i in x:
	if i == 0 or i == n - 1:
		ans = 2
		break
for i in y:
	if i == 0 or i == m - 1:
		ans = 2
		break
print(ans)


(n, m) = list(map(int, input().split()))
A = []
for i in range(n):
	A.append(list(map(int, input().split())))
for i in range(n):
	for j in range(m):
		if A[i][j] == 1 and (i in [0, n - 1] or j in [0, m - 1]):
			print(2)
			return
print(4)


t = input().split()
n = int(t[0])
m = int(t[1])
slon = 9999
for i in range(n):
	t = input().split()
	for j in range(m):
		if t[j] == '1':
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				slon = 2
			else:
				slon = min(4, slon)
print(slon)


(n, m) = map(int, input().split())
ans = 4
for i in range(n):
	line = list(map(int, input().split()))
	for j in range(m):
		if line[j] == 1 and (i == 0 or i == n - 1):
			ans = 2
		if line[j] == 1 and (j == 0 or j == m - 1):
			ans = 2
print(ans)


def table(n_int: int, m_int: int, table_cells_list: list) -> int:
	boundary_bool = False
	for i in range(n_int):
		for j in range(m_int):
			if table_cells_list[i][j] == 1:
				if i == 0 or i == n_int - 1 or j == 0 or (j == m_int - 1):
					boundary_bool = True
	if boundary_bool:
		return 2
	else:
		return 4
(n, m) = list(map(int, input().split()))
table_cells = [list(map(int, input().split())) for i in range(n)]
print(table(n, m, table_cells))


(n, m) = map(int, input().split())
flag = 0
for i in range(n):
	l = list(map(int, input().split()))
	if i == 0 or i == n - 1:
		if 1 in l:
			flag = 1
			break
	elif l[0] == 1 or l[m - 1] == 1:
		flag = 1
		break
if flag == 1:
	print('2')
else:
	print('4')


def table(n, m, arr):
	d = []
	for i in range(n):
		for j in range(m):
			if arr[i][j] == 1:
				d.append((i, j))
	for i in d:
		if i[0] == 0 or i[0] == n - 1 or i[1] == 0 or (i[1] == m - 1):
			return 2
	return 4
(n, m) = map(int, input().split())
arr = []
for i in range(n):
	row = [int(x) for x in input().split()]
	arr.append(row)
print(table(n, m, arr))


(n, m) = (int(i) for i in input().split())
l = []
for i in range(n):
	l.append([int(i) for i in input().split()])
c = 0
for i in range(m):
	if l[0][i] == 1:
		c = 1
		break
for i in range(n):
	if l[i][0] == 1:
		c = 1
		break
for i in range(n):
	if l[i][m - 1] == 1:
		c = 1
		break
for i in range(m):
	if l[n - 1][i] == 1:
		c = 1
		break
if c == 1:
	print(2)
else:
	print(4)


def Horizontal(R, n, m):
	for i in range(0, m):
		if R[0][i] == 1 or R[n - 1][i] == 1:
			return True
	return False

def Vertical(R, n, m):
	for i in range(0, n):
		if R[i][0] == 1 or R[i][m - 1] == 1:
			return True
	return False

def Respuesta(R, n, m):
	if R[0][0] == 1 or R[n - 1][0] == 1 or R[0][m - 1] == 1 or (R[n - 1][m - 1] == 1):
		return 1
	elif Horizontal(R, n, m) or Vertical(R, n, m):
		return 2
	else:
		return 4
L = input().split()
n = int(L[0])
m = int(L[1])
R = []
for i in range(0, n):
	A = input().split()
	A = list(map(int, A))
	R.append(A)
print(Respuesta(R, n, m))


(n, m) = map(int, input().split())
arr = []
x = 0
for i in range(n):
	l = [int(i) for i in input().split()]
	arr.append(l)
for i in range(n):
	for j in range(m):
		if i == 0 and arr[0][j] == 1:
			x = 2
			break
		elif j == 0 and arr[i][j] == 1:
			x = 2
			break
		elif i == n - 1 and arr[n - 1][j] == 1:
			x = 2
			break
		elif j == m - 1 and arr[i][m - 1] == 1:
			x = 2
			break
if x == 2:
	print(x)
else:
	print(4)


nm = list(map(int, input().strip().split()))
n = nm[0]
m = nm[1]
val = False
table = []
for i in range(n):
	row = list(map(int, input().strip().split()))
	table.append(row)
for i in [0, n - 1]:
	for j in range(m):
		if table[i][j] == 1:
			val = True
			break
if not val:
	for j in [0, m - 1]:
		for i in range(1, n - 1):
			if table[i][j] == 1:
				val = True
				break
if val:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
l = []
for j in range(n):
	l.append(list(map(int, input().split())))
r = []
k = 0
for j in range(len(l)):
	if l[j][0] == 1:
		k = k + 1
	if l[j][-1] == 1:
		k = k + 1
if 1 in l[0]:
	k = k + 1
if 1 in l[n - 1]:
	k = k + 1
if k > 0:
	print(2)
else:
	print(4)


(m, n) = map(int, input().split())
lst = []
count = 0
cond = False
for x in range(m):
	arr = list(map(int, input().split()))
	lst.append(arr)
if 1 in lst[count] or 1 in lst[m - 1]:
	cond = True
mst = [x[0] for x in lst]
cst = [x[-1] for x in lst]
if 1 in mst or cond == True or 1 in cst:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
ans = 4
import sys
for i in range(n):
	l1 = list(map(int, input().split()))
	if i == 0 or i == n - 1:
		if l1.count(1) > 0:
			ans = 2
			print(2)
			sys.exit()
	elif l1[0] == 1 or l1[m - 1] == 1:
		print(2)
		sys.exit()
print(4)


(n, m) = list(map(int, input().rstrip().split()))
mat = []
count1 = 0
count2 = 0
for i in range(n):
	li = list(map(int, input().rstrip().split()))
	if i == 0 or i == n - 1:
		for j in li:
			if j == 1:
				count1 += 1
	if li[0] == 1 or li[m - 1] == 1:
		count2 += 1
if count1 > 0 or count2 > 0:
	print(2)
else:
	print(4)


(r, c) = map(int, input().split())
one = []
table = []
for x in range(r):
	row = list(map(int, input().split()))
	table.append(row)
for i in range(r):
	for j in range(c):
		if table[i][j] == 1:
			one.append([i + 1, j + 1])
side = False
for i in range(len(one)):
	try:
		if one[i][0] == 1 or one[i][0] == r:
			side = True
			break
		elif one[i][1] == 1 or one[i][1] == c:
			side = True
			break
	except IndexError:
		pass
if side == False:
	ans = 4
if side == True:
	ans = 2
print(ans)


(a, b) = map(int, input().split())
rowList = []
ideal = False
for i in range(a):
	rowList.append([int(x) for x in input().split()])
for j in range(len(rowList[0])):
	if rowList[0][j] == 1:
		ideal = True
for k in range(len(rowList[a - 1])):
	if rowList[a - 1][k] == 1:
		ideal = True
for l in range(len(rowList)):
	if rowList[l][0] == 1:
		ideal = True
	if rowList[l][b - 1] == 1:
		ideal = True
if ideal:
	print(2)
else:
	print(4)


input_str = input()
(n, m) = (int(input_str.split()[0]), int(input_str.split()[1]))
a = []
points_wall = []
points = 0
temp = list(map(int, input().split()))
a.append(temp)
pos = temp.index(1) if temp.count(1) else -1
if pos != -1:
	points_wall.append([pos, 0])
for i in range(n - 2):
	temp = list(map(int, input().split()))
	if temp[0]:
		points_wall.append([0, i + 1])
	if temp[m - 1]:
		points_wall.append([m - 1, i + 1])
	points += temp[1:m - 1].count(1)
	a.append(temp)
temp = list(map(int, input().split()))
a.append(temp)
pos = temp.index(1) if temp.count(1) else -1
if pos != -1:
	points_wall.append([pos, n - 1])

def count(matr, x):
	c = 0
	for i in range(len(matr)):
		c += matr[i].count(x)
	return c
if len(points_wall):
	print(2)
elif points:
	print(4)


(n, m) = map(int, input().split())
c = 0
for i in range(n):
	a = list(map(int, input().split()))
	if i == 0 and 1 in a:
		c = 1
	elif i == n - 1 and 1 in a:
		c = 1
	elif a[0] == 1 or a[m - 1] == 1:
		c = 1
if c == 1:
	print(2)
else:
	print(4)


(n, m) = [int(i) for i in input().split()]
mat = []
for i in range(n):
	l = [int(i) for i in input().split()]
	mat.append(l)
l = []
for i in range(0, n):
	for j in range(0, m):
		if mat[i][j] == 1 and (i == 0 or i == n - 1 or (j == 0 or j == m - 1)):
			l.append(2)
		elif mat[i][j] == 1:
			l.append(4)
print(min(l))


from sys import stdin, stdout
(n, m) = map(int, stdin.readline().split())
ans = 4
for i in range(1, n + 1):
	ind = 1
	for v in list(map(int, stdin.readline().split())):
		if v and (ind == 1 or ind == m or i == 1 or (i == n)):
			ans = 2
		ind += 1
stdout.write(str(ans))


(n, m) = map(int, input().split())
arr = []
for z in range(n):
	arr.append([])
	arr[z] = [int(i) for i in input().split()]
c = 0
for i in range(n):
	for j in range(m):
		if arr[i][j] == 1:
			if i == 0 or i == n - 1:
				c = 1
			if j == 0 or j == m - 1:
				c = 1
if c:
	print(2)
else:
	print(4)


input_string = input().split()
n = int(input_string[0])
m = int(input_string[1])
x = []
for i in range(0, n):
	a = input().split()
	x.append(a)
ans = 4
for i in range(0, n):
	if ans == 1:
		break
	for j in range(0, m):
		if x[i][j] == '1':
			if i == 0 and j == 0 or (i == 0 and j == m - 1) or (i == n - 1 and j == 0) or (i == n - 1 and j == m - 1):
				ans = 1
				break
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				ans = 2
print(ans)


(n, m) = map(int, input().split())
l = []
k = 0
for _ in range(n):
	l.append(list(map(int, input().split())))
for i in range(n):
	for j in range(m):
		if i == 0 or j == 0 or i == n - 1 or (j == m - 1):
			if l[i][j] == 1:
				k = 1
				break
if k:
	print('2')
else:
	print('4')


(n, m) = map(int, input().split())
a = [input().split() for _ in range(n)]
b = list(zip(*a))
print((4, 2)[sum([x.count('1') for x in [a[0], a[-1], b[0], b[-1]]]) > 0])


from math import sqrt
(n, m) = map(int, input().split())
found = False

def solve(n, m):
	for i in range(n):
		s = input().split()
		for (j, elem) in enumerate(s):
			if elem != '0':
				if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
					return 2
	return 4
print(solve(n, m))


(n, m) = list(map(int, input().split()))
A = [input().split() for i in range(n)]
T = False
for i in range(n):
	for j in range(m):
		if A[i][j] == '1' and (i == 0 or j == 0 or i == n - 1 or (j == m - 1)):
			T = True
if T:
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
s = []
for i in range(n):
	x = list(map(str, input().split()))
	for j in range(len(x)):
		if x[j] == '1':
			if i == 0 or j == 0 or i == n - 1 or (j == m - 1):
				s.append(2)
			else:
				s.append(4)
print(min(s))


(n, m) = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(n)]
for i in range(1, m - 1):
	if mat[0][i] is 1:
		print(2)
		exit(0)
for i in range(1, n - 1):
	if mat[i][0] is 1:
		print(2)
		exit(0)
for i in range(1, n - 1):
	if mat[i][m - 1] is 1:
		print(2)
		exit(0)
for i in range(1, m - 1):
	if mat[n - 1][i] is 1:
		print(2)
		exit(0)
print(4)


inp = input().split()
(n, m) = [int(i) for i in inp]
mat = []
for i in range(n):
	inp = input().split()
	mat.append(inp)
for i in range(n):
	if mat[i][0] == '1' or mat[i][m - 1] == '1':
		print(2)
		exit()
for i in range(m):
	if mat[0][i] == '1' or mat[n - 1][i] == '1':
		print(2)
		exit()
print(4)


import math

def main_function():
	(n, m) = [int(i) for i in input().split(' ')]
	table = [[int(k) for k in input().split(' ')] for i in range(n)]
	good_cell_found_on_the_corner = False
	for i in range(n):
		for j in range(m):
			if (i == 0 or j == 0 or i == n - 1 or (j == m - 1)) and table[i][j] == 1:
				good_cell_found_on_the_corner = True
	if not good_cell_found_on_the_corner:
		print(4)
	else:
		print(2)
main_function()


(n, m) = map(int, input().split())
matrix = [[] for i in range(n)]
for i in range(n):
	matrix[i] = list(map(int, input().split()))
flag = 0
for i in range(n):
	for j in range(m):
		if matrix[i][j] == 1:
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				flag = 1
if flag == 0:
	print(4)
else:
	print(2)


import sys
import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list or type(n) is str:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return int(str(n)[::-1])

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True
(n, m) = mi()
t = []
for i in range(n):
	t.append(li())
two = False
for i in range(n):
	for j in range(m):
		if t[i][j] == 1 and (i == 0 or i == n - 1 or j == 0 or (j == m - 1)):
			print(2)
			exit()
print(4)


(n, m) = [int(x) for x in input().split()]
rows = [[int(x) for x in input().split()] for _ in range(n)]
if 1 in rows[0] or 1 in rows[-1] or any((rows[i][0] == 1 for i in range(n))) or any((rows[i][-1] == 1 for i in range(n))):
	print(2)
else:
	print(4)


tableData = input().split(' ')
rows = int(tableData[0])
cols = int(tableData[1])
table = []
for r in range(rows):
	table.append(input().split(' '))
found = False
for i in range(rows):
	if table[i][0] == '1' or table[i][cols - 1] == '1':
		found = True
		break
if not found:
	for i in range(cols):
		if table[0][i] == '1' or table[rows - 1][i] == '1':
			found = True
			break
if found:
	print('2')
else:
	print('4')


(n, m) = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
	if (i == 0 or i == n - 1) and sum(l[i]):
		print(2)
		break
	elif l[i][0] == 1 or l[i][-1] == 1:
		print(2)
		break
else:
	print(4)


(n, m) = map(int, input().split())
list1 = []
for i in range(n):
	ll = list(map(int, input().split()))
	list1.append(ll)
f = 0
for i in range(n):
	for j in range(m):
		if list1[i][j] == 1:
			if i == 0 or i == n - 1 or j == 0 or (j == m - 1):
				f = 1
if f == 0:
	print(4)
else:
	print(2)


(n, m) = [int(x) for x in input().split()]
list = []
for _ in range(n):
	list.append([int(x) for x in input().split()])
for _ in range(1, n - 1, 1):
	if list[_][0] == 1 or list[_][-1] == 1:
		print(2)
		exit()
for _ in range(1, m - 1, 1):
	if list[0][_] == 1 or list[-1][_] == 1:
		print(2)
		exit()
print(4)


(n, m) = map(int, input().split())
rows = []
cols = [''] * m
for i in range(n):
	row = input().split()
	rows.append(row)
	for j in range(m):
		cols[j] += row[j]
if '1' in rows[0] or '1' in cols[0] or '1' in cols[-1] or ('1' in rows[-1]):
	print(2)
else:
	print(4)


(r, c) = list(map(int, input().split()))
l1 = []
for _ in range(r):
	l = list(map(int, input().split()))
	l1.append(l)
fc = []
lc = []
for i in range(r):
	fc.append(l1[i][0])
for i in range(r):
	lc.append(l1[i][-1])
a = str(l1[0])
b = str(l1[-1])
if a.count('1') or b.count('1') or fc.count(1) or lc.count(1):
	print(2)
else:
	print(4)


row = input().split()
n = int(row[0])
m = int(row[1])
border = False
for i in range(n):
	newRow = input().split()
	if i == 0 or i == n - 1:
		if '1' in newRow:
			border = True
	elif newRow[0] == '1' or newRow[m - 1] == '1':
		border = True
if border:
	print('2')
else:
	print('4')


(n, m) = map(int, input().split())
ans = 4
if '1' in input():
	ans = 2
else:
	for i in range(n - 2):
		t = input()
		if '1' == t[0] or '1' == t[-1]:
			ans = 2
			break
if '1' in input():
	ans = 2
print(ans)


(n, m) = map(int, input().split())
values = []
for i in range(n):
	values.append([int(k) for k in input().split()])

def checkRows(x, n):
	return 1 in x[0] or 1 in x[n - 1]

def checkCol(x, n, m):
	are = False
	for i in range(n):
		if x[i][0] == 1 or x[i][m - 1] == 1:
			are = True
			break
	return are
if checkRows(values, n) or checkCol(values, n, m):
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
l = []
for i in range(n):
	x = list(map(int, input().split()))
	l.append(x)
x1 = []
y1 = []
for i in range(n):
	for j in range(m):
		if l[i][j] == 1:
			x1.append(i)
			y1.append(j)
if 0 in x1 or 0 in y1 or m - 1 in y1 or (n - 1 in x1):
	print(2)
else:
	print(4)


(n, m) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
(col0, col1) = ([], [])
for i in A:
	col0.append(i[0])
	col1.append(i[-1])
if A[0][0] + A[-1][-1] == 2 or A[0][-1] + A[-1][0] == 2:
	print(1)
elif 1 in A[0] or 1 in A[-1] or 1 in col0 or (1 in col1):
	print(2)
else:
	print(4)


(n, m) = [int(i) for i in input().split()]
top = [int(i) for i in input().split()]
mid = [int(j) for i in range(1, n - 1) for j in input().split()]
bot = [int(i) for i in input().split()]
l = (n - 2) * m
mid = mid[0:l:m] + mid[m - 1:l:m]
print(2) if 1 in top + mid + bot else print(4)