Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	for stdin.Scan() {
		n, _ := strconv.Atoi(stdin.Text())
		if n == 0 {
			break
		}

		a := make([][]int, n+1)
		for i := 0; i < n; i++ {
			sum := 0
			stdin.Scan()
			a[i] = make([]int, n+1)
			str := strings.Split(stdin.Text(), " ")
			for j := 0; j < n; j++ {
				a[i][j], _ = strconv.Atoi(str[j])
				sum += a[i][j]
				fmt.Printf("%5d", a[i][j])
			}
			a[i][n] = sum
			fmt.Printf("%5d\n", a[i][n])
		}
		a[n] = make([]int, n+1)
		for i := 0; i <= n; i++ {
			sum := 0
			for j := 0; j < n; j++ {
				sum += a[j][i]
			}
			a[n][i] = sum
			fmt.Printf("%5d", a[n][i])
		}
		fmt.Println()
	}
}

C++: // #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef map<int, int> MAPII;
typedef vector<pair<int, int> > VPII;

#define MP make_pair
#define fastIO	cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (未test)
// #define FOREACH_IT(it,c)	for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(), c_end=c.end(); it!=c_end; ++it)

// ------------------- include, typedef, define END. -------------------

void solve(VVI &in, int n){
	int rowSum = 0, columnSum = 0;
	int allSum=0;
	FOR(i, 0, in.size()-1){
		rowSum = 0;
		columnSum = 0;
		FOR(j, 0, in[i].size()-1){
			rowSum += in[i][j];
			columnSum += in[j][i];
		}
		allSum += rowSum;
		in[i][n] = rowSum;
		in[n][i] = columnSum;
	}
	in[n][n] = allSum;
	FOR(i, 0, in.size()){
		FOR(j, 0, in[i].size()){
			printf("%5d", in[i][j]);
		}
		puts("");
	}
}

int main(){
	fastIO;
	int n, in = 0;
	while (cin >> n, n){
		VVI inNum(n + 1, VI(n + 1));
		FOR(i, 0, n){
			FOR(j, 0, n){
				cin >> in;
				inNum[i][j] = in;
			}
		}
		solve(inNum,n);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n,c;
  while(cin>>n,n){
    int a[n+1][n+1];
    rep(i,n){c=0;rep(j,n){
        cin>>a[i][j];
        c+=a[i][j];
      }
      a[i][n]=c;
    }
    rep(j,n){c=0;rep(i,n){
      c+=a[i][j];
    }
    a[n][j]=c;
  }
  rep(i,n+1){rep(i,n+1){cout<<a[i][j];}cout<<endl;}
  }
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	int n;

	while (cin >> n, n)
	{
		int table[11][11] = {};

		for (int y = 0; y < n; y++)
		{
			for (int x = 0; x < n; x++)
			{
				cin >> table[x][y];
			}
		}

		for (int y = 0; y < n; y++)
		{
			for (int x = 0; x < n; x++)
			{
				table[n][y] += table[x][y];
			}
		}
		for (int x = 0; x < n; x++)
		{
			for (int y = 0; y < n; y++)
			{
				table[x][n] += table[x][y];
			}
		}
		for (int x = 0; x < n; x++)
		{
			table[n][n] += table[x][n];
		}

		for (int y = 0; y <= n; y++)
		{
			for (int x = 0; x <= n; x++)
			{
				printf("%5d", table[x][y]);
			}
			cout << endl;
		}
	}

	return 0;
}

C++: #include <cstdio>

int main(){
  int n;
  
  while(1){
    scanf("%d", &n);
    if(!n) break;
    
    int table[11][11] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
	scanf("%d", &table[i][j]);
      }
    }
    
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
	table[i][n] += table[j][i];
	table[n][i] += table[i][j];
      }
    }
    
    for(int i=0; i<n; i++)
      table[n][n] += table[n][i];
    
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++)
	printf("%5d", table[i][j]);
      putchar('\n');
    }
  }
    
  return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;

int main(){
	int n;
	int matrix[100][100];
	while(cin >> n){
		if(n==0) return 0;
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				cin >> matrix[i][j];
			}
		}
		int sumX[100],sumY[100],sum=0;
		for(int i=0; i<n; i++){
			sumX[i] = matrix[0][i] + matrix[1][i] + matrix[2][i] + matrix[3][i];
			sumY[i] = matrix[i][0] + matrix[i][1] + matrix[i][2] + matrix[i][3];
			sum+=sumX[i];
		}
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				printf("%5d",matrix[i][j]);
			}
			printf("%5d\n",sumY[i]);
		}
		for(int i=0; i<n; i++) printf("%5d",sumX[i]);
		printf("%5d\n",sum);
		
	}
}

C++: #include <stdio.h>
int main(){
	int n;
	while(1){
	    int l,r;
		int a[11][11];
		scanf("%d",&n);
		if(!n) break;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++) scanf("%d",&a[i][j]);
		}
		for(int i=0;i<n+1;i++){
			l=r=0;
			for(int j=0;j<n;j++){
				l+=a[i][j];
				r+=a[j][i];
			}
			a[i][n]=l;
			a[n][i]=r;
		}
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++) printf("%5d ",a[i][j]);
			printf("\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
using namespace std;

int main(){
	int data[11][11];
	int n;
	cin >> n;
	while (n){

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> data[i][j];
			}
		}
		int sum_col[10] = { 0 };
		int sum_low[10] = { 0 };

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum_col[i] += data[i][j];
				sum_low[i] += data[j][i];
			}
			data[i][n] = sum_col[i];
			data[n][i] = sum_low[i];
		}
		int sum = 0;
		for (int i = 0; i < n; i++){
			sum += data[i][n];
		}
		data[n][n] = sum;
		for (int i = 0; i < n + 1; i++){
			for (int j = 0; j < n + 1; j++){
				cout << setw(5) << right << data[i][j];
			}
			cout<<"\n";
		}
		cin >> n;
	}
	
	return 0;	
}

C++: #include <bits/stdc++.h>

#define loop(v, f, l) for(int v = (f), v##_ = (l); v < v##_; ++v)

using namespace std;

typedef long long int lint;
static inline int in(){ int x; scanf("%d", &x); return (x); }
static inline lint inl(){ lint x; scanf("%lld", &x); return (x); }

int matrix[11][11];

int main()
{
	int n;

	while( (n = in()) != 0 )
	{
		loop(i, 0, 11)
		{
			loop(j, 0, 11)
			{
				matrix[i][j] = 0;
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][j] = in();
			}
		}
		
		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][n] += matrix[i][j];
				matrix[n][i] += matrix[j][i];
			}
			matrix[n][n] += matrix[i][n];
		}
		
		loop(i, 0, n + 1)
		{
			loop(j, 0, n + 1)
			{
				printf("%5d ", matrix[i][j]);
			}
			printf("\n");
		}
		
	}

	return (0);
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
#define MAX 11

int main(){
    int n,mat[MAX][MAX];
    
    while(true){
        for(int i = 0 ; i < MAX ; i++)
            for(int j = 0 ; j < MAX ; j++)
                mat[i][j] = 0;
        
        cin >> n;
        if(!n) break;
        
        for(int i = 0 ; i < n ; i++)
            for(int j = 0 ; j < n ; j++)
                cin >> mat[i][j];
        
        int sum = 0;
        for(int i = 0 ; i <= n ; i++){
            for(int j = 0 ; j <= n ; j++){
                sum += mat[i][j];
                if(j == n){
                    mat[i][j] = sum;
                    sum = 0;
                }
                
            }
        }
        
        for(int j = 0 ; j <= n ; j++){
            for(int i = 0 ; i <= n ; i++){
                sum += mat[i][j];
                if(i == n){
                    mat[i][j] = sum;
                    sum = 0;
                }
            }
        }
        
        int e = 0;
        for(int i = 0 ; i < n ; i++){
            e += mat[n][i];
            if(i == n-1) mat[n][n] = e;
        }
        
        for(int i = 0 ; i <= n ; i++){
            for(int j = 0 ; j <= n ; j++){
                if(!j) printf("%5d" ,mat[i][j]);
                else printf(" %4d" ,mat[i][j]);
            }
            cout << endl;
        }
    }
    
    return 0;
}