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: interview

Starter code:

Solutions:
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 = 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=list(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 = 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)


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 = map(int, input().split())
k = 0
for i in range(n):
    a = list(map (int, input().split()))
    for j in range(m):
        if a[j] == 1 and (j in [0, m-1] or i in [0, n-1]):
            k=1
           
if k == 1:
    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)


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)


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")


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())
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())
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)

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
    
    
# 1
if len(points_wall):
    print (2)
else:
    if points:
        print (4)

def solve(a):
	check2 = [x + y for x, y in zip(a[0], a[-1])]
	if any(check2):
		return 2
	checkn = [sum(l) for l in zip(*a)]
	if any(check2) and any(checkn):
		return 3
	return 4

n, m = [int(x) for x in input().split()]
a = []
for i in range(n):
	a.append([int(x) for x in input().split()])
print(min(solve(a), solve(list(zip(*a)))))


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()

'''
Created on Jan 28, 2015

@author: mohamed265
'''
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)


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)

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)


data = input().split()

N, M = int(data[0]), int(data[1])

table = []
for i in range(N):
    table.append(input().split())

answer = 0

## for first row
if any(e == '1' for e in table[0]):
    answer = 2
##  for last row
elif any(e == '1' for e in table[-1]):
    answer = 2
## for first and  last column
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 = list(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));







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)

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 = list(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)
        return
for i in range(1, n - 1):
    if mat[i][0] is 1:
        print(2)
        return
for i in range(1, n - 1):
    if mat[i][m - 1] is 1:
        print(2)
        return
for i in range(1, m - 1):
    if mat[n - 1][i] is 1:
        print(2)
        return
print(4)


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))