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 <iomanip>
using namespace std;

int main() {
	int N;

	while (cin >> N && N != 0) {
		int dataset[N+1][N+1];
        for (int i = 0; i != N; ++i) {
            for (int j = 0; j != N; ++j) {
                cin >> dataset[i][j];
            }
        }

        for (int i = 0; i != N; ++i) {
            int total = 0;
            for (int j = 0; j != N; ++j) {
                total += dataset[i][j];
            }
            dataset[i][4] = total;
        }

        for (int i = 0; i != N; ++i) {
            int total = 0;
            for (int j = 0; j != N; ++j) {
                total += dataset[j][i];
            }
            dataset[4][i] = total;
        }

        int ans = 0;
        for (int i = 0; i != N; ++i) {
            ans += dataset[4][i];      
        }
        dataset[4][4] = ans;

        for (int i = 0; i != N+1; ++i) {
            for (int j = 0; j != N; ++j) {
                cout << setw(5) << dataset[i][j];
            } 
            cout << setw(5) << dataset[i][4] << endl;
        }
	}
    return 0;
}

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

using namespace std;

int main(){
  int n;
  while(cin>>n&&n){
    vector<int> data((n+1)*(n+1));
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin>>data[i*(n+1)+j];
        data[i*(n+1)+n]+=data[i*(n+1)+j];
        data[n*(n+1)+j]+=data[i*(n+1)+j];
        data[(n+1)*(n+1)-1]+=data[i*(n+1)+j];
      }
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
        cout<<setw(5)<<data[i*(n+1)+j];
      }
      cout<<endl;
    }
  }
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	while (cin >> n&&n) {
		int g[15][15];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> g[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			int sum = 0;
			for (int j = 0; j < n; j++) {
				sum += g[i][j];
			}
			g[i][n] = sum;
		}
		for (int j = 0; j < n; j++) {
			int sum = 0;
			for (int i = 0; i < n; i++) {
				sum += g[i][j];
			}
			g[n][j] = sum;
		}
		int ssum = 0;
		for (int i = 0; i < n; i++) {
			ssum += g[i][n];
		}
		g[n][n] = ssum;
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				if (g[i][j] < 10)cout << "    " << g[i][j];
				else if (g[i][j] < 100)cout << "   " << g[i][j];
				else if (g[i][j] < 1000)cout << "  " << g[i][j];
				else if (g[i][j] < 10000)cout << " " << g[i][j];
			}
			cout << endl;
		}
	}
}

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

int main(int argc, char *argv[]){
  int n;
  cin >> 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 <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include <iomanip>

int main(int argc, char* argv[])
{
    while (true) {
        std::string line;
        std::stringstream buffer;
        getline(std::cin, line);
        buffer << line;
        size_t length = 0;
        buffer >> length;
        if (length == 0) break;
        int *total = new int[length + 1];
        size_t width = length + 1;
        memset(total, 0, sizeof(int) * width);
        for (size_t i = 0; i < length; ++i) {
            getline(std::cin, line);
            std::istringstream s(line);
            std::string element;
            int sum = 0;
            for (size_t n = 0; n < length; ++n) {
               getline(s, element, ' ');
               std::stringstream tmp;
                int val = 0;
                tmp << element;
                tmp >> val;
                sum += val;
                total[n] += val;
                std::cout << std::setw(5) << val;
            }
            std::cout << std::setw(5) << sum << std::endl;
            total[length] += sum;
        }
        for (size_t i = 0; i < width; ++i) {
            std::cout << std::setw(5) << total[i];
        }
        std::cout << std::endl;
        delete[] total;
    }
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(void) {
  int n;
  int data[100][100];

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

    data[n][n] = 0;
    for(int i = 0; i < n; i++) {
      data[i][n] = 0;
      for(int j = 0; j < n; j++) {
	data[i][n] += data[i][j];
      }
      data[n][n] += data[i][n];
    }
    for(int j = 0; j < n; j++) {
      data[n][j] = 0;
      for(int i = 0; i < n; i++) {
	data[n][j] += data[i][j];
      }
    }
    
    for(int i = 0; i <= n; i++) {
      for(int j = 0; j <= n; j++) {
	printf("%5d", data[i][j]);
      }
      cout << endl;
    }
  }
  return 0;
}

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

int main(){
    int n = 0;
    while (1){
	cin >> n;
	if (!n) break;
	vector<vector<int> > matrix(n+1, vector<int>(n+1));
	int input;
	for (int a = 0; a < n; a++){
	    matrix[a][n] = 0;
	    for (int b = 0; b < n; b++){
		cin >> matrix[a][b];
		matrix[a][n] += matrix[a][b];
	    }
	}
	for (int a = 0; a < n; a++){
	    for (int b = 0; b < n; b++){
		matrix[n][b] += matrix[a][b];
	    }
	}
	for (int b = 0; b < n; b++)
	    matrix[n][n] += matrix[n][b];

	for (int a = 0; a < n+1; a++){
	    for (int b = 0; b < n+1; b++)
		cout << right << setw(5) << matrix[a][b];
	    cout << '\n';
	}
    }
}