""" 二维投影到一维,简化时间复杂度,O(n^2) 在X,Y维求距离和,相加即为二维的距离和,在合适的地方取最小值。 """ import sys if __name__ == '__main__': # sys.stdin = open("input.txt", "r") n = int(input()) matrix = [] for _ in range(n): matrix.append(list(map(int, input().strip().split()))) x, y, space = [0] * n, [0] * n, [] for i in range(n): for j in range(n): if matrix[i][j] == 1: x[i] += 1 y[j] += 1 else: space.append([i, j]) disX, disY = [], [] for i in range(n): tmpX, tmpY = 0, 0 for j in range(n): tmpX += abs(j - i) * x[j] tmpY += abs(j - i) * y[j] disX.append(tmpX) disY.append(tmpY) ans = sys.maxsize for i, j in space: ans = min(ans, disX[i] + disY[j]) print(ans if ans != sys.maxsize else -1)