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<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
int main(){
	int ta[100][100],sum[100];
	int n,all;
	while(cin>>n,n){
	all=0;
	for(int i=0;i<100;i++)sum[i]=0,fill(ta[i],ta[i]+100,0);
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>ta[i][j];
	for(int i=0;i<n;i++){
		int tnow=0;
		for(int j=0;j<n;j++){
		string ss=itos(ta[i][j]);
		for(int i=0;i<5-ss.size();i++)cout<<" ";	
		cout<<ta[i][j];
		sum[j]+=ta[i][j];
		tnow+=ta[i][j];
		all+=ta[i][j];
		}
		string ss=itos(tnow);
		for(int i=0;i<5-ss.size();i++)cout<<" ";
		cout<<tnow<<endl;
	}
	for(int i=0;i<n;i++){
	string ss=itos(sum[i]);
	if(i==0)for(int i=0;i<5-ss.size();i++)cout<<" ";
	cout<<sum[i];

	for(int i=0;i<5-ss.size();i++)cout<<" ";
	}
	cout<<all<<endl;
	}

	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int i,j;
	int a[10+1][10+1];
	
	scanf("%d",&n);
	
	for (i=0; i<=n; i++) 
		for (j=0; j<=n; j++)
			a[i][j]=0;
	
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&a[i][j]);
		}
	}
	for (i=0; i<n; i++) 
		for (j=0; j<n; j++) 
			a[i][n]+=a[i][j];
	
	for (i=0; i<n; i++) 
		for (j=0; j<n; j++) 
			a[n][j]+=a[i][j];
	
	for (i=0; i<n; i++) 
		a[n][n]+=a[i][n];
	
	for (i=0; i<=n; i++) {
		for (j=0; j<=n; j++) 
			printf ("%5d",a[i][j]);
		printf ("\n");
	}
	return 0;
}

C++: import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        int sub;
        int sum;
        int all;

        while(s.hasNext()){
            sub = s.nextInt();
            all = 0;
            if(sub == 0)
                break;
            int[][] table = new int[sub][sub];
            for(int i = 0; i < sub; i++){
                sum = 0;
                for(int j = 0; j < sub; j++){
                    table[i][j] = s.nextInt();
                    sum += table[i][j];
                    System.out.printf("%5d",table[i][j]);
                    if(j == sub - 1){
                        System.out.printf("%5d", sum);
                        System.out.println();
                    }
                }
            }

            for(int j = 0; j < sub; j++){
                sum = 0;
                for(int i = 0; i < sub; i++){
                    sum += table[i][j];
                }
                all += sum;
                System.out.printf("%5d", sum);
            }
            System.out.printf("%5d", all);
            System.out.println();
        }
        s.close();
    }

}

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

int main()
{
	int n, i, j;
	vector< vector<int> > list(11, vector<int>(11));
	while (cin>>n, n) {
		for (i = 0; i <= n; i++) {
			list[i][n] = 0;
			list[n][i] = 0;
		}
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				cin>>list[i][j];
				list[i][n] += list[i][j];
				list[n][j] += list[i][j];
				list[n][n] += list[i][j];
			}
		}
		for (i = 0; i <= n; i++) {
			for (j = 0; j <= n; j++) {
				cout<<setw(5)<<right<<list[i][j];
			}
			cout<<endl;
		}
	}

	return 0;
}

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

int main(){
	int n;
	while(cin >> n){
		if(n == 0) break;
		else{
			int data[n+2][n+2];

			for (int i = 0; i < n+2; ++i){
				for(int j = 0; j < n+2; j++){					
				data[i][j] = 0;
				}
			}
			
			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+1; ++i){
				for(int j = 0; j < n; j++){	
					data[n][i] += data[j][i];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n+1; j++){
					cout.width(5);
					cout << data[i][j];
					// if (j != n) cout<<" ";
				}
				cout << endl;
			}
				
		}
	}
	return 0;
}

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("%4d", field[i][0]);
      for (int j=1; j<n+1; j++) {
        printf(" %4d", field[i][j]);
      }
      puts("");
    }
  }
}

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

int n;

int solve(int n){
        int format=0,table[1000][1000]={};
        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++){
                        table[n][j] += table[i][j];
                        table[i][n] += table[i][j];
                }
        }
        for(int i=0;i<n;i++){
                table[n][n] += table[i][n];
                table[n][n] += table[n][i];
        }
        table[n][n] /= 2;
        if(table[n][n] < 10){
                format=1;
        }else if(table[n][n] < 100){
                format=2;
        }else if(table[n][n] < 1000){
                format=3;
        }else if(table[n][n] < 10000){
                format=4;
        }
        for(int i=0;i<n+1;i++){
                for(int j=0;j<n+1;j++){
                        cout << setw(format) << table[i][j];
                        cout << " ";
                }
                cout << "" << endl;
        }
}

int main(){
        while(1){
                cin >> n;
                if(n == 0){
                        break;
                }
                solve(n);
        }
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    int matrix[11][11] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
	cin >>matrix[i][j];
      }
    }
    int suma = 0;
    for(int i=0,sumc,sumr; i<n; i++){
      sumc = 0;
      sumr = 0;
      for(int j=0; j<n; j++){
	sumc+=matrix[i][j];
	sumr+=matrix[j][i];
      }
      suma+=sumc;
      matrix[i][n] = sumc;
      matrix[n][i] = sumr;
    }
    matrix[n][n] = suma;
    for(int i=0; i<=n; i++){
      for(int j=0; j<=n; j++){
	printf("%5d",matrix[i][j]);
      }
      cout <<endl;
    }
  }
  return 0;
}