import numpy as np N = int(input()) grid = [] for i in range(N): row = [int(c) for c in input().split(' ')] grid.append(row) grid = np.array(grid) j_s = np.tile(np.arange(N), (N,1)) i_s = np.tile(np.arange(N).reshape(N, 1), N) x_s = np.tile(np.arange(N).reshape(N, 1, 1), (1, N, N)) distances = np.where((i_s + j_s >= x_s) & (i_s - j_s <= x_s), j_s, abs(x_s - i_s)) # 各社員に対して、最適な攻撃位置を求める optimal_sum = 0 for k in range(N): ks_house_distance = np.min(np.sum(np.where(grid == k+1, 1, 0) * distances, axis=(1,2))) optimal_sum += np.min(ks_house_distance) print(optimal_sum)