import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MazeMin {
static int[][] dir = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
static int[][] maze;
static int minmize;
static int[][] queue;
static int head;
static int tail;
static int step;
static int N;
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
sc = new Scanner(new File("src/file/mazemin"));
N = sc.nextInt();
minmize = N * N;
maze = new int[N + 2][N + 2];
queue = new int[N * N][4];
for (int i = 0; i < N + 2; i++) {
maze[i][0] = -1;
maze[0][i] = -1;
maze[N + 1][i] = -1;
maze[i][N + 1] = -1;
}
for (int i = 1; i < N + 1; i++) {
for (int j = 1; j < N + 1; j++) {
maze[i][j] = sc.nextInt();
}
}
head = tail = step = 0;
maze[2][1] = 2;
queue[tail++] = new int[] { 2, 1, step, 1 };
Loop();
System.out.println(queue[head][2] + 1);
System.out.println(queue[head][3]);
}
private static void Loop() {
// TODO Auto-generated method stub
while (true) {
if (queue[head][0] == N && queue[head][1] == N)
break;
while (true) {
if (queue[head][0] == N && queue[head][1] == N)
break;
if (queue[head][2] == step) {
int nx, ny, tstep;
for (int l = 0; l < 4; l++) {
nx = queue[head][0] + dir[l][0];
ny = queue[head][1] + dir[l][1];
tstep = queue[head][3];
if (maze[nx][ny] == 1) {
maze[nx][ny] = step + 3;
queue[tail++] = new int[] { nx, ny, step + 1, tstep };
} else if (maze[nx][ny] == step + 3) {
for (int i = tail - 1; i > head; i--) {
if (queue[i][0] == nx && queue[i][1] == ny) {
queue[i][3] += tstep;
}
}
}
}
head++;
} else
break;
}
step++;
}
}
}
sample input:
//1为可走,0不可走
10
1 1 0 1 1 0 0 1 0 1
1 1 0 1 0 1 0 0 0 1
1 1 1 1 1 1 1 0 0 0
0 1 0 1 1 0 1 1 0 0
0 1 1 0 0 0 0 1 1 0
1 1 0 1 1 1 0 1 0 1
0 1 1 1 1 1 1 1 0 1
1 0 0 0 0 0 0 1 0 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 1 1
sample output:
19
6