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++: 4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0

C++: #include <iostream>
#include <iomanip>

int main()
{
	while (true) {
		int size;
		std::cin >> size;
		if (size == 0) break;
		
		int** table = new int*[size+1];
		for (int i=0; i<size+1; i++) {
			table[i] = new int[size+1];
		}
		
		for (int i=0; i<size; i++) {
			for (int j=0; j<size; j++) {
				std::cin >> table[i][j];
			}
		}
		
		for (int i=0; i<size; i++) {
			int row_sum = 0;
			for (int j=0; j<size; j++) row_sum += table[i][j];
			table[i][size] = row_sum;
		}
		for (int j=0; j<=size; j++) {
			int column_sum = 0;
			for (int i=0; i<size; i++) column_sum += table[i][j];
			table[size][j] = column_sum;
		}
		
		for (int i=0; i<=size; i++) {
			for (int j=0; j<=size; j++) {
				std::cout.setf(std::ios::right, std::ios::adjustfield);
				std::cout << std::setw(5) << table[i][j];
			}
			std::cout << std::endl;
		}
		
		for (int i=0; i<size+1; i++) delete[] table[i];
		delete[] table;
	}
	
	return 0;
}

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

int main(){
	int n,i,j;
	bool flag = false;
	while(cin >> n && n){
		int table[11][11] = {{0}};
		for(i = 0;i < n;i++){
			for(j = 0;j < n;j++){
				cin >> table[i][j];
				table[i][n] += table[i][j];
				table[n][j] += table[i][j];
			}
			table[n][n] += table[i][n];
		}
		if(flag) cout << "\n";
		for(i = 0;i <= n;i++){
			for(j = 0;j <= n;j++){
				if(table[i][j] / 100 != 0) cout << "  ";
				else cout << "   ";
				cout << table[i][j];
			}
			cout << "\n";
		}
		flag = true;
	}
	return 0;
}

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

int main(){
  int n;

  while(cin>>n,n){
    int t[n+1][n+1];
    memset(t,0,sizeof(t));
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin>>t[i][j];
        t[i][n] += t[i][j];
        t[n][j] += t[i][j];
        t[n][n] += t[i][j];
      }
    }

    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
        printf("%5d",t[i][j]);
      }
      printf("\n");
    }
  }
}

C++: #include <iostream>
#include <cstdio>
const int MAX_N = 10;
using namespace std;

int main(void) {
  int n;
  while (1) {
    cin >> n;
    if (n == 0) break;
    int masu[MAX_N + 1][MAX_N + 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> masu[i][j];
      }
    }

    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        tmp += masu[i][j];
      }
      masu[i][n] = tmp;
    }
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        tmp += masu[j][i];
      }
      masu[n][i] = tmp;
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += masu[n][i];
    }
    masu[n][n] = sum;

    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n; j++) {
        printf("%5d", masu[i][j]);
      }
      printf("%5d\n", masu[i][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];
			}
			if (i != n)	cout<<"\n";
		}
		cin >> n;
		if (n) cout<<"\n";
	}
	
	return 0;	
}

C++: #include <iostream>
#include <cstdio>
#include <string>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

int main(){
    int n;
    while (cin >> n, n){
        int v[10] = {};
        rep(i, n){
            int s = 0;
            rep(j, n){
                int t; cin >> t;
                printf("%5d", t);
                s += t;
                v[j] += t;
            }
            printf("%5d\n", s);
        }
        int s = 0;
        rep(i, n){
            printf("%5d", v[i]);
            s += v[i];
        }
        printf("%5d\n", s);
    }
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))



int c[100000][11][11];
int main(){
	CLR(c);
	int cnt=0;
	vector<int>res;
	while(1){
		int N;
		cin>>N;
		if(N==0)break;
		res.PB(N);
		int sumr=0;
		REP(i,N){
			REP(j,N){
				cin>>c[cnt][i][j];
				c[cnt][i][N]+=c[cnt][i][j];
				c[cnt][N][j]+=c[cnt][i][j];
			}
			c[cnt][N][N]+=c[cnt][i][N];
		}
		cnt++;
	}
	REP(i,cnt){
		REP(j,res[i]+1){
			REP(k,res[i]+1){
				cout<<setw(5)<<right<<c[i][j][k];
			}
			cout<<endl;
		}
	}
	return 0;
}

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

int main(){
  int n;
  while(1){
    cin >> n;
    if(n==0)break;

    int x[15][15]={};
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
	cin >> x[i][j];
      }
    }
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
	x[i][n] += x[i][j];
	x[n][j] += x[i][j];
	x[n][n] += x[i][j];
      }
    }
    
    for(int i = 0; i <= n; i++){
      for(int j = 0; j <= n; j++){
	if(j < n){
	  cout.width(5);
	  cout << x[i][j];
	}
	if(j==n){
	  cout.width(5);
	  cout << x[i][j] << "\n";
	}
      }
    }
  }
  
  return 0;
}