import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.System;
import java.util.Arrays;
public class D_Grid_Java {
public static void main(String[] Args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int GridSize = Integer.parseInt(br.readLine());
int GridSizeSq = GridSize * GridSize;
int[][] Grid1 = new int[GridSizeSq][3];
// int[][] SortedGrid = Grid1;
int paths = 0;
int counter = 0;
for (int i = 0; i < GridSize; i++) {
int[] NodeStates = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
for (int j : NodeStates) {
Grid1[counter] = new int[] { counter, j, 1 };
counter += 1;
}
}
int[][] Grid = Grid1.clone();
Arrays.sort(Grid1, (a, b) -> Integer.compare(a[1], b[1]));
// Arrays.sort(SortedGrid, (a,b) ->Integer)
for (int[] member : Grid1) {
// CheckDirections(member[0], Grid1, paths, GridSize);
int position = member[0];
// int[][] Grid = Grid1;
int y = (position / GridSize);
int x = position - y * GridSize;
int pathsInMe = Grid[position][2];
// Check up
for (int offset = 1; offset < y + 1; offset++) {
if (Grid[position - GridSize * offset][1] > Grid[position][1]) {
Grid[position - GridSize * offset][2] += pathsInMe;
}
}
// Check down
for (int offset = 1; offset < GridSize - y; offset++) {
if (Grid[position + GridSize * offset][1] > Grid[position][1]) {
Grid[position + GridSize * offset][2] += pathsInMe;
}
}
// Check left
for (int offset = 1; offset < x + 1; offset++) {
if (Grid[position - offset][1] > Grid[position][1]) {
Grid[position - offset][2] += pathsInMe;
}
}
// Check right
for (int offset = 1; offset < GridSize - x; offset++) {
if (Grid[position + offset][1] > Grid[position][1]) {
Grid[position + offset][2] += pathsInMe;
}
}
paths += Grid[position][2];
}
System.out.println(paths);
}
// public static void CheckDirections(int position,int[][] Grid,int paths, int
// GridSize){
// }
}