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;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) break;
    int v[n + 1][n + 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> v[i][j];
      }
    }
    // x
    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = 0; j < n; j++) {
        sum += v[i][j];
      }
      v[i][n] = sum;
    }
    // y
    for (int j = 0; j < n; j++) {
      int sum = 0;
      for (int i = 0; i < n; i++) {
        sum += v[i][j];
      }
      v[n][j] = sum;
    }
    int k = 0;
    for (int i = 0; i < n; i++) {
      k += v[n][i];
    }
    v[n][n] = k;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        if (j == n) {
          cout << v[i][j] << endl;
        } else {
          cout << v[i][j] << " ";
        }
      }
    }

  }
}

C++: #include <cstdio>
#include <vector>
using namespace std;
int main() 
{
    int n;
    while (scanf("%d",&n)&&n!=0) {
        vector<vector<int> > map;
        for (int i=0; i<n; i++) {
            vector<int> in(n+1);
            in[n]=0;
            for (int j=0; j<n; j++) {
                scanf("%d",&in[j]);
                in[n]+=in[j];
            }
            map.push_back(in);
        }
        vector<int> sum(n+1,0);
        for (int i=0; i<n+1; i++) {
            for (int j=0; j<n; j++) {
                sum[i]+=map[j][i];
            }
        }
        map.push_back(sum);
        for (int i=0; i<n+1; i++) {
            for (int j=0; j<n+1; j++) {
                printf("%5d",map[i][j]);
            }
            printf("\n");
        }
    }
}

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

int main(){
	int n;
	int area[11][11];
	while(cin >> n){
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cin >> area[i][j];
			}
		}
		for(int i = 0; i < n; i++){
			int count = 0;
			for(int j = 0; j < n; j++){
				count += area[i][j];
			}
			area[i][n] = count;
		}
		for(int j = 0; j <= n; j++){
			int count = 0;
			for(int i = 0; i < n; i++){
				count += area[i][j];
			}
			area[n][j] = count;
		}
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				cout << setw(4) << area[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main() {
	int n;
	while (cin >> n, n) {
		int data[150][150] = { 0 };

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

		}
		for (int i = 0;i < n+1;i++) {
			for (int j = 0;j < n + 1;j++) {
				if (data[i][j] < 10) {
					cout << "    " << data[i][j];
				}
				else if (data[i][j] < 100) {
					cout << "   " << data[i][j];
				}
				else if (data[i][j] < 1000) {
					cout << "  " << data[i][j];
				}
			}
			cout << endl;
		}
	}
	return 0;

}

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

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

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

    for (int i = 0; i < n+1; ++i) {
      int sum = 0;
      for (int j = 0; j < n; ++j)
	sum += table[j][i];
      table[n][i] = sum;
    }

    for (int i = 0; i < n+1; ++i) {
      for (int j = 0; j < n+1; ++j)
	printf("%5d", table[i][j]);
      puts("");
    }
  }
  return 0;
}

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

int main(){
    int n;
    int *table;
    while(1){
        std::cin >> n;
        if(!n){
            return 0;
        }
        table = new int [(n + 1) * (n + 1)];
        for(int i = 0; i < n * n; i++){
            std::cin >> table[i / n * (n + 1) + i % n];
        }
        for(int r = 0; r < n; r++){
            for(int c = 0; c < n; c++){
                table[r * (n + 1) + n] += table[r * (n + 1) + c];
                table[n * (n + 1) + c] += table[r * (n + 1) + c];
                table[n * (n + 1) + n] += table[r * (n + 1) + c];
            }
        }
        
        for(int i = 0; i < (n + 1) * (n + 1); i++){
            std::cout << std::setw(5) << std::right << table[i];
            if(i % (n + 1) == n){
                std::cout << std::endl;
            }
        }
        delete [] table;
    }
}

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

int n;
int field[1000][1000];
int total;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) return 0;

    total = 0;
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
        scanf("%d", &field[i][j]);
        total += field[i][j];
      }
    }

    for (int i=0; i<n; i++) {
      int sum = 0;
      for (int j=0; j<n; j++) {
        sum += field[i][j];
      }
      field[i][n] = sum;
    }
    field[n][n] = total;

    for (int j=0; j<n; j++) {
      int sum = 0;
      for (int i=0; i<n; i++) {
        sum += field[i][j];
      }
      field[n][j] = sum;
    }

    for (int i=0; i<n+1; i++) {
      printf("%5d", field[i][0]);
      for (int j=1; j<n+1; j++) {
        printf("%5d", field[i][j]);
      }
      puts("");
    }
  }
}

C++: #include <bits/stdc++.h>

#define loop(v, f, l) for(int v = (f), v##_ = (l); v < v##_; ++v)

using namespace std;

typedef long long int lint;
static inline int in(){ int x; scanf("%d", &x); return (x); }
static inline lint inl(){ lint x; scanf("%lld", &x); return (x); }

int matrix[11][11];

int main()
{
	int n;

	while( (n = in()) != 0 )
	{
		loop(i, 0, 11)
		{
			loop(j, 0, 11)
			{
				matrix[i][j] = 0;
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][j] = in();
			}
		}
		
		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][n] += matrix[i][j];
				matrix[n][i] += matrix[j][i];
			}
			matrix[n][n] += matrix[i][n];
		}
		
		loop(i, 0, n + 1)
		{
			loop(j, 0, n + 1)
			{
				printf("%5d", matrix[i][j]);
			}
			printf("\n");
		}
		
	}

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