#include <stdio.h>

int main() {
    int N, Diferentes=0;
    scanf("%d", &N);

    int V[N][N], Soma[2*N+2];
    //Zerando Todos os valores dos vetores da soma.
    for(int i=0; i<2*N+2; i++){
        Soma[i]=0;
    }
    
    //Leitura de variaveis
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            scanf("%d", &V[i][j]);
        }
    }
    //Soma das linhas do quadrado
    for(int i=0; i<N; i++){
       for(int j=0; j<N; j++){
           Soma[i]+=V[i][j];
       }
    }
    //Soma das colunas do quadrado
    for(int j=0; j<N; j++){
        for(int i=0; i<N; i++){
            Soma[j+N]+=V[i][j];
        }
    }
   //Soma Diagonal Principal
    for(int i=0;i<N; i++){
        Soma[2*N]+=V[i][i];
    }
    //Soma diagonal secundaria
    for(int i=0; i<N; i++){
        Soma[2*N+1]+=V[i][N-i-1];
    }
    //Comparando vetores
    for(int i=1; i<2*N+2; i++){
            if(Soma[0]!=Soma[i]){
                Diferentes=1;
                break;
            }
        }
    
    if(Diferentes==0){
        printf("%d", Soma[0]);
    }
    else{
        printf("-1");
    }
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: