dx=[0,1,0,-1]
dy=[-1,0,1,0]
n = int(input())
board = [ [0]*n for i in range(n) ]
dq = deque()
MAX = -999
MIN = 999
cnt = 0
for i in range(n): # 한 줄씩 입력 받기
tmp = list(map(int,input().split()))
for j in range(n):
board[i][j] = tmp[j]
if tmp[j] < MIN :
MIN = tmp[j]
sx =i
sy =j
dq.append([sx,sy])
elif tmp[j]>MAX:
MAX = tmp[j]
ex = i
ey = j
while dq:
tmp = dq.popleft()
#
if tmp[0] == ex and tmp[1]==ey:
cnt+=1
for a in range(4):
X = dx[a]+tmp[0]
Y = dy[a]+ tmp[1]
if 0<=X<n and 0<=Y<n and board[X][Y] > board[tmp[0]][tmp[1]] :
# board[X][Y] = 0
dq.append([X,Y])
print(cnt)
'''
해당 코드에서 뭐가 잘못된건지 집어주시면 감사하겠습니다.
'''