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>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
	const int MAX_N = 20;
	int N;
	int table[MAX_N][MAX_N];
	int keta[MAX_N][MAX_N];
	int sum[MAX_N], retu;
	int res;

	while (1) {
		cin >> N;
		if (N == 0)break;
		for (int i = 0;i < N;i++) {
			for (int j = 0;j < N;j++) {
				cin >> table[i][j];
			}
		}

		for (int i = 0;i < N;i++) {
			for (int j = 0;j < N;j++) {
				keta[i][j] = (int)log10(table[i][j]) + 1;
			}
		}

		for (int i = 0;i < N;i++) {
			sum[i] = 0;
			for (int j = 0;j < N;j++) {
				for (int k = 0;k < 5 - keta[i][j];k++) {
					cout << " ";
				}
				cout << table[i][j];
				sum[i] += table[i][j];
			}
			for (int k = 0;k < 5 - (log10(sum[i]) + 1);k++) {
				cout << " ";
			}
			cout << sum[i] << endl;
		}
		//last
		for (int j = 0;j < N;j++) {
			retu = 0;
			for (int i = 0;i < N;i++) {
				retu += table[i][j];
			}
			for (int k = 0;k < 5 - (log10(retu) + 1);k++) {
				cout << " ";
			}
			cout << retu;
		}
		res = 0;
		for (int i = 0;i < N;i++) {
			res += sum[i];
		}
		for (int k = 0;k < 5 - (log10(res) + 1);k++) {
			cout << " ";
		}
		cout << res << endl;
	}

	return 0;
}

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

int main(int argc, char *argv[]){
  int n;
  while(cin >> n,n){
    int mat[n + 1][n + 1];
    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++) {
      int hor = 0;
      int var = 0;
      for (int j = 0; j < n; j++) {
        hor += mat[i][j];
        var += mat[j][i];
      }
      mat[i][n] = hor;
      mat[n][i] = var;
      sum += hor;
    }
    mat[n][n] = sum;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        printf("%5d", mat[i][j]);
      }
      std::cout << std::endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int main(){
	int n[127], tn=0,ni=0,all = 0, i = 0;
	int data[1024][10];
	int tdata[10] = { 0 };
	
	//input
	while (1){
		cin >> n[ni];
		if (n[ni] == 0)break;
		tn += n[ni];

		for (; i < tn; i++){
			for (int j = 0; j < n[ni]; j++){
				cin >> data[i][j];
			}
		}
		ni++;
	}
	
	//output
	for (int k = 0; k < ni; k++){
		for (int i = 0; i < n[k]; i++, all = 0){
			for (int j = 0; j < n[k]; j++){
				printf(" %5d", data[i][j]);
				all += data[i][j];
				tdata[j] += data[i][j];
			}
			printf(" %5d\n", all);
		}

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

		for (int i = 0; i < n[k]; i++){
			tdata[i] = 0;
		}
		all = 0;
	}
	return 0;
}

C++: #include<ios>
#include<iostream>
using namespace std;
int n,a[11][11];
main()
{
    while(cin>>n,n)
    {
        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++)
        {
            int sum=0;
            for(int j=0;j<n;j++)sum+=a[i][j];
            a[i][n]=sum;
        }
        for(int j=0;j<=n;j++)
        {
            int sum=0;
            for(int i=0;i<n;i++)sum+=a[i][j];
            a[n][j]=sum;
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)printf("%5d",a[i][j]);
            puts("");
        }
    }
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
template<typename V> constexpr typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  while (cin >> n, n) {
    int t[n + 1][n + 1];
    rep(i, n) rep(j, n) cin >> t[i][j];
    rep(i, n) {
      int c = 0, d = 0;
      rep(j, n) c += t[i][j], d += t[j][i];
      t[i][n] = c;
      t[n][i] = d;
    }
    int e = 0;
    rep(i, n) e += t[n][i];
    t[n][n] = e;
    rep(i, n + 1) {
      rep(j, n + 1) cout << setw(5) << t[i][j];
      cout << endl;
    }
  }
}

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

int main()
{
	int data[11][11] = { 0 };
	int n;
	while (cin>>n)
	{
		if (n == 0)break;

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

		for (int i = 0; i < n; i++)          //??????????¨????
		{
			for (int j = 0; j < n; j++)
			{
				data[i][n] += data[i][j];
			}
		}

		for (int i = 0; i < n; i++)           //??????????¨????
		{
			for (int j = 0; j < n+1; j++)     //n??????????????????
			{
				data[n][j] += data[i][j];
			}
		}


		for (int i = 0; i < n+1; i++)
		{
			for (int j = 0; j < n+1; j++)
			{
				cout.width(5);
				cout << data[i][j];
			}
			cout << endl;
		}
	}


	return 0;
}

C++: #include<iostream>
#include<vector>
int main()
{
	while (1) {
		int num;
		std::cin >> num;
		if (num == 0)break;
		std::vector<std::vector<int>>vec(num + 1, std::vector<int>(num + 1));
		for (int i = 0; i < num; ++i) {
			for (int j = 0; j < num; ++j) {
				std::cin >> vec[i][j];
			}
		}
		for (int i = 0; i < num; ++i) {
			for (int j = 0; j < num; ++j) {
				vec[num][i] += vec[j][i];
				vec[i][num] += vec[i][j];
			}
		}
		for (int i = 0; i < num + 1; ++i) {
			for (int j = 0; j < num + 1; ++j) {
				if (j == num)std::cout << vec[i][j] << std::endl;
				else std::cout << vec[i][j] << " ";
			}
		}
	}
	return 0;
}