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 <bits/stdc++.h>

using namespace std;

#define MAX 11
 
int main()
{
    int n, mat[MAX][MAX];
     
    while (cin >> n, n) {
        for (int i = 0; i < MAX; i++) {
            for (int j = 0; j < MAX; j++) {
                mat[i][j] = 0;
	    }
	}
         
        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++) {
		printf("%5d", mat[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];
    fgets(line,sizeof(line),stdin);
    sscanf(line,"%d",&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++){
      //  char buf[100];
      // fgets(buf,sizeof(buf),stdin);
      for(int j=0;j<n;j++){
	//sscanf(buf,"%d",&list[i][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 <iostream>
#include <iomanip>
#include <vector>

void process_table(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    if (n <= 0)
        return;
    std::vector<int> col_sums(n, 0);
    for (int i = 0; i < n; ++i) {
        int row_sum = 0;
        for (int j = 0; j < n; ++j) {
            int x;
            in >> x;
            col_sums[j] += x;
            row_sum += x;
            out << std::setw(5) << x;
        }
        out << std::setw(5) << row_sum << '\n';
    }
    int sum = 0;
    for (auto const& col_sum: col_sums) {
        out << std::setw(5) << col_sum;
        sum += col_sum;
    }
    out << std::setw(5) << sum << '\n';
}

int main() {
    while (std::cin) {
        process_table(std::cin, std::cout);
    }
}

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

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

void print(int a,bool f){
  cout << setw(5) << right << a;
  if(f)cout << endl;
}

int main(){
  int n;
  int a[10][10];
  int row[10],col;

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

    for(int i=0;i<n;i++){
      col = 0;
      for(int j=0;j<n;j++){
	row[j] += a[i][j];
	col += a[i][j];
	print(a[i][j],0);
      }
      print(col,1);
    }

    col = 0;
    for(int i=0;i<n;i++){
      col += row[i];
      print(row[i],0);
    }
    print(col,1);
  }
}

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

using namespace std;

string disp(int a){
	string ans = "     ";
	ans[0] = (a / 10000) + '0';
	ans[1] = (a / 1000) % 10 + '0';
	ans[2] = (a / 100) % 10 + '0';
	ans[3] = (a / 10) % 10 + '0';
	ans[4] = a % 10 + '0';
	
	for(int i = 0; i <= 4; i++){
		if(ans[i] == '0'){
			ans[i] = ' ';
		}else{
			break;
		}
	}
	
	return ans;
}

int main(){
	
	int n;
	
	int mat[20][20];
	
	cin >> n;
	
	while(n != 0){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cin >> mat[i][j];
			}
		}
		
		int sum;
		
		for(int i = 0; i < n; i++){
			sum = 0;
			for(int j = 0; j < n; j++){
				sum += mat[i][j];
			}
			mat[i][n] = sum;
		}
		
		for(int j = 0; j <= n; j++){
			sum = 0;
			for(int i = 0; i < n; i++){
				sum += mat[i][j];
			}
			mat[n][j] = sum;
		}
		
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				cout << disp(mat[i][j]);
			}
			cout << endl;
		}
		
		cin >> n;
	}
	return 0;
}

C++: #include <cstdio>

int matrix[12][12];
int n;

int main() {
  while(scanf("%d", &n) != EOF, n) {
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
	scanf("%d", &matrix[i][j]);
      }
    }

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

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

C++: #include <cstdio>

int main(){
	int n, table[20][20];

	while( scanf("%d", &n) , n ){
		for(int i=0 ; i<20 ; i++){
			for(int j=0 ; j<20 ; j++){
				table[i][j] = 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[i][j];
				table[n][j] += 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]);
			}
			printf("\n");
		}

	}

	return 0;
}

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

using namespace std;

int main(){

	int N;
	while(cin>>N,N){
		vector<int> vi(N*N,0),vi2(N*2+1,0);
		for(int i=0;i<N;i++){
			int sum=0;
			for(int j=0;j<N;j++){
				int num;
				cin>>num;
				vi[i*N+j]=num;
				sum+=num;
			}
			vi2[i]=sum;
		}
		for(int i=0;i<N;i++){
			int sum=0;
			for(int j=0;j<N;j++){
				sum+=vi[i+j*N];
				printf("%5d",vi[i*N+j]);
			}
			printf("%5d\n",vi2[i]);
			vi2[N*2]+=vi2[i];
			vi2[N+i]=sum;
		}
		for(int i=0;i<=N;i++){
			printf("%5d",vi2[N+i]);
		}
		cout<<endl;
	}

	return 0;
}