Go: package main

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

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

		input := make([][]int, n+1, n+1)
		totalRow := make([]int, n+1, n+1)

		for i := 0; i < n; i++ {
			row := make([]int, n+1, n+1)
			scanner.Scan()

			row = numbers(scanner.Text())

			input[i] = row
			total(totalRow, row)
		}

		input[n] = totalRow

		for xk := range input {
			for yk, yv := range input[xk] {
				if yk == n {
					fmt.Printf("%5d\n", yv)
				} else {
					fmt.Printf("%5d", yv)
				}
			}
		}
	}
}

func total(sum, row []int) {
	for i := 0; i < len(row); i++ {
		sum[i] += row[i]
	}
}

func numbers(s string) []int {
	var n []int
	var sum int
	for _, f := range strings.Fields(s) {
		i, err := strconv.Atoi(f)
		if err == nil {
			n = append(n, i)
			sum += i
		}
	}
	n = append(n, sum)
	return n
}

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

int a[11][11];

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

    for (i = 0; i < n; i++){
      for (j = 0; j <= n; j++){
        a[i][n] += a[i][j];
      }
    }
    for (j = 0; j <= n; j++){
      for (i = 0; i <= n; i++){
        a[n][j] += a[i][j];
      }
    }
    for (i = 0; i <= n; i++){
      a[i][n] /= 2;
    }
    for (j = 0; j <= n; j++){
      a[n][j] /= 2;
    }

    for (i = 0; i <= n; i++){
      cout << a[i][0];
      for (j = 1; j <= n; j++){
        cout << " " << a[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	while (n != 0) {
		int a[10][10] = {}, sum_h[11] = {}, sum_v[11] = {};
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> a[i][j];
				printf("%5d",a[i][j]);
				sum_h[i] += a[i][j];
				sum_v[j] += a[i][j];
			}
			sum_v[n] += sum_h[i];
			printf("%5d\n", sum_h[i]);
		}
		for (int i = 0; i < n; i++)
			printf("%5d", sum_v[i]);
		printf("%5d\n", sum_v[n]);
		cin >> n;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
	int dp[15][15];
	int n;
	while(true){
	cin>>n;
	if(n==0)
		break;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			cin>>dp[i][j];
	int x=0;
	for(int i=0;i<n;i++){
		int h=0;
		for(int j=0;j<n;j++)
			h+=dp[i][j];
		dp[i][n]=h;
		x+=h;
	}
	for(int i=0;i<n;i++){
		int h=0;
		for(int j=0;j<n;j++)
			h+=dp[j][i];
		dp[n][i]=h;
	}
	dp[n][n]=x;
	for(int i=0;i<=n;i++){
		for(int j=0;j<=n;j++)
			cout<<dp[i][j]<<" ";
		cout<<endl;
	}
	}
	return 0;
}

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;
}