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++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int A[15][15] = {};
	for (int n; cin >> n&&n;) {
		memset(A, 0, sizeof(A));
		rep(i, 0, n) {
			rep(j, 0, n) {
				cin >> A[i][j];
				A[i][n] += A[i][j];
				A[n][j] += A[i][j];
				A[n][n] += A[i][j];
			}
		}
		rep(i, 0, n + 1) {
			rep(j, 0, n + 1) {
				cout << setfill(' ') << setw(5) << A[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}

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

int hoge(int w);
int getKetasu(int n);

int main(){
	int w;
	
	while(1){
	 cin>>w;
	 if(w==0){break;}
	 hoge(w);
	}
	
	return 0;
}

int hoge(int w){	//?????£???????????????
	int t[100][100];
	int x,y,i;
	
	for(y=0;y<w;y++){
	 for(x=0;x<w;x++){
	  cin>>t[x][y];
	 }
	}
	
	for(y=0;y<w;y++){
	 t[w][y]=0;
	 for(x=0;x<w;x++){
	  t[w][y]+=t[x][y];
	 }
	}
	for(x=0;x<=w;x++){
	 t[x][w]=0;
	 for(y=0;y<w;y++){
	  t[x][w]+=t[x][y];
	 }
	}
	
	for(y=0;y<=w;y++){
	 for(x=0;x<=w;x++){
	  for(i=getKetasu(t[x][y]);i<5;i++){cout<<" ";}
	  cout<<t[x][y];
	 }
	 cout<<endl;
	}
	
	return 0;
}

int getKetasu(int n){	//?????°?????????
	if(n<10){
	 return 1;
	}else if(n<100){
	 return 2;
	}else if(n<1000){
	 return 3;
	}else{
	 return 4;
	}
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>

int main() {
  int n;
  while(cin >>n && n){
    vector< vector<int> > v(n, vector<int>(n));
    REP(i, n) REP(j, n) cin >>v[i][j];
    REP(i, n){
      v[i].push_back(accumulate(v[i].begin(), v[i].end(), 0));
      REP(j, v[i].size()){
        printf("%5d", v[i][j]);
      }
      cout <<endl;
    }
    int ss = 0;
    REP(j, n){
      int s = 0;
      REP(i, n) s += v[i][j];
      printf("%5d", s);
      ss += s;
    }
    printf("%5d", ss);
    cout <<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
	{
        int table[11][11]={0};
        if(n==0)
			break;
        for(int i=0,in;i<n;++i)
		{
            for(int j=0;j<n;++j)
			{
                cin>>in;
                table[i][j]=in;
                table[n][j]+=in;
                table[i][n]+=in;
            }
        }
        for(int i=0;i<n;++i)table[n][n]+=table[n][i];
        for(int i=0;i<n+1;++i)
		{
            for(int j=0;j<n+1;++j)printf("%5d",table[i][j]);
            cout<<endl;
        }
    }
    return 0;
}

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

using namespace std;

int main(){
  while(1){
    int n;
    char line[100];
    cin>>n;
    if(n==0)return 0;
    int list[11][11];
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	list[i][j]=0;
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin>>list[i][j];
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	list[i][n]+=list[i][j];
	list[n][i]+=list[j][i];
      }
    }
    for(int i=0;i<n;i++){
      list[n][n]+=list[n][i];
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	printf("%5d",list[i][j]);
      }
      printf("\n");
    }
  }
}

C++: #include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0) { break; }

    vector<vector<int>> matrix;
    for (int i = 0; i < n; ++i) {
      matrix.emplace_back(vector<int>(n));
      for (int j = 0; j < n; ++j) { scanf("%d", &matrix[i][j]); }
    }

    vector<int> col_sums(n);
    for (const auto& row : matrix) {
      int row_sum = accumulate(row.cbegin(), row.cend(), 0);
      for (int i = 0; i < n; ++i) {
        printf("%5d", row[i]);
        col_sums[i] += row[i];
      }
      printf("%5d\n", row_sum);
    }
    for (const auto elm : col_sums) { printf("%5d", elm); }
    printf("%5d\n", accumulate(col_sums.cbegin(), col_sums.cend(), 0));
  }

  return 0;
}

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

int main(){
  int n;
  while(cin>>n){
    int a[11][11],b[10]={},c[10]={},d=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	cin>>a[i][j];
    
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++){
	b[i]+=a[i][j];
	c[i]+=a[j][i];
      }
    for(int i=0;i<n;i++)
      d+=b[i];
    for(int i=0;i<n+1;i++){
      a[i][n]=b[i];
      a[n][i]=c[i];
      a[n][n]=d;
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++)
	printf("%5d",a[i][j]);
      cout<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(true){
  cin>>n;
  if(!n)break;
  int table[n+1][n+1];
  for(int y=0;y<n;y++){
    int s = 0;
    for(int x=0;x<n;x++){
      cin>>table[x][y];
      s+=table[x][y];
    }
    table[n][y] = s;
  }
  for(int x=0;x<n+1;x++){
    int s = 0;
    for(int y=0;y<n;y++){
      s+=table[x][y];
    }
    table[x][n] = s;
  }
  for(int y=0;y<n+1;y++){
    for(int x=0;x<n;x++){
      printf("% 4d ",table[x][y]);
    }
    printf("% 4d\n",table[n][y]);
  }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n!=0){
    int hyou[n][n];
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin>>hyou[i][j];
      }
    }
    int hyoup[n+1][n+1]={0};
    hyoup[0][n]=0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	hyoup[i][j]=hyou[i][j];
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	hyoup[i][n]+=hyou[i][j];
	hyoup[n][i]+=hyou[j][i];
      }
    }
    for(int i=0;i<n;i++)
      hyoup[n][n]+=hyoup[n][i];
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	printf("%5d",hyoup[i][j]);
      }
      cout<<endl;
    }
  }
  return 0;
}