# Arup Guha
# Magic Square Checker
# 10/10/2018

# Checks to see if the values in grid are 1 to N^2.
def checknum(grid):

    freq = [0]*(len(grid)*len(grid)+1)

    # Go through the whole grid.
    for i in range(len(grid)):
        for j in range(len(grid[i])):

            # Out of range; bad numbers.
            if grid[i][j] < 1 or grid[i][j] > len(grid)*len(grid):
                return False

            # Update the frequency of this number.
            freq[grid[i][j]] += 1

            # Can't work.
            if freq[grid[i][j]] > 1:
                return False

    # Valid numbers, if we get here.
    return True

# Checks to see if each row adds to magicsum.
def checkrows(grid,magicsum):

    for i in range(len(grid)):

        # Add up row i.
        tot = 0;
        for j in range(len(grid[i])):
            tot += grid[i][j]

        # This row is bad!
        if tot != magicsum:
            return False

    # If we made it here, we're good!
    return True

# Checks to see if each row adds to magicsum.
def checkcols(grid,magicsum):

    for j in range(len(grid[0])):

        # Add up row i.
        tot = 0;
        for i in range(len(grid)):
            tot += grid[i][j]

        # This row is bad!
        if tot != magicsum:
            return False

    # If we made it here, we're good!
    return True

# Checks to see if both diagonals in this square add up to the magic square or not.
def checkdiag(grid,magicsum):

    # Add up forward diagonal.
    tot = 0
    for i in range(len(grid)):
        tot += grid[i][i]

    # Forward diag didn't work.
    if tot != magicsum:
        return False

    # Add up Backwards Diagonal
    tot = 0;
    for i in range(len(grid)):
        tot += grid[-i-1][i]

    # This is our result.
    return tot == magicsum

# Returns true iff all sums work out in grid.
def checksum(grid):

    # Mathematically calculate magic sum.
    n = len(grid)
    magicsum = n*(n*n+1)//2

    # See if all the sum checks work or not.
    return checkrows(grid, magicsum) and checkcols(grid, magicsum) and checkdiag(grid, magicsum)

# Wrapper function that checks to see if this works!
def checksquare(grid):
    return checknum(grid) and checksum(grid)

def main():

    grid = []
    n = int(input("How many rows are in your magic square?\n"))

    # Read in each row.
    for i in range(n):

        # The really cool way to do this =)
        # tmp = [int(i) for i in input("").split()]

        # Split and convert to ints.
        tmp = input("").split()
        for i in range(len(tmp)):
            tmp[i] = int(tmp[i])

        # Add to temp.
        grid.append(tmp)

        
    # Output the result.
    if checksquare(grid):
        print("We have a magic square!")
    else:
        print("Sorry, no magic square.")

main()
