num=[]
while gets
    x=[]
    y=[]
    z=0
    s=$_.to_i
    break if (s==0)
    
    s.times do |i|
        x[i]=0
        num<<gets.chomp.split(nil).map(&:to_i)
        s.times do |j|
            x[i]+=num[i][j]
        end
        z+=x[i]
    end
    s.times do |i|
        y[i]=0
        s.times do |j|
            y[i]+=num[j][i]
        end
    end
    s.times do |i|
        s.times do |j|
            printf("%5d",num[i][j])
        end
        printf("%5d\n",x[i])
    end
    s.times do |i|
        printf("%5d",y[i])
    end
    printf("