Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	for stdin.Scan() {
		n, _ := strconv.Atoi(stdin.Text())
		if n == 0 {
			break
		}

		a := make([][]int, n+1)
		for i := 0; i < n; i++ {
			sum := 0
			stdin.Scan()
			a[i] = make([]int, n+1)
			str := strings.Split(stdin.Text(), " ")
			for j := 0; j < n; j++ {
				a[i][j], _ = strconv.Atoi(str[j])
				sum += a[i][j]
				fmt.Printf("%5d", a[i][j])
			}
			a[i][n] = sum
			fmt.Printf("%5d\n", a[i][n])
		}
		a[n] = make([]int, n+1)
		for i := 0; i <= n; i++ {
			sum := 0
			for j := 0; j < n; j++ {
				sum += a[j][i]
			}
			a[n][i] = sum
			fmt.Printf("%5d", a[n][i])
		}
		fmt.Println()
	}
}

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]);
	for(int i=0;i<5-ss.size();i++)cout<<" ";
	cout<<sum[i];
	if(i==n-1){ss=itos(all);for(int i=0;i<5-ss.size();i++)cout<<" ";}
	}
	cout<<all<<endl;
	}

	return 0;
}

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

int main(){
	int n;
	while(std::cin>>n,n){
		int data[5][5];
		memset(data,0,sizeof(data));
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				std::cin>>data[i][j];
				data[i][4]+=data[i][j];
				data[4][j]+=data[i][j];
				data[4][4]+=data[i][j];
			}
		}
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				std::cout<<data[i][j]<<" ";
			}
			std::cout<<std::endl;
		}
	}
	return 0;
}

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

using namespace std;


int main() {
	int n;
	while (cin >> n, n != 0) {
		vector<vector<int>> vec(n + 1);

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				int temp;
				cin >> temp;
				vec[i].push_back(temp);
			}
			int sum = accumulate(vec[i].begin(), vec[i].end(), 0);
			vec[i].push_back(sum);
		}
		for (int j = 0; j < n + 1; ++j) {
			int sum = 0;
			for (int i = 0; i < n; ++i) {
				sum += vec[i][j];				
			}
			vec[n].push_back(sum);
		}
		for (auto& a : vec) {
			for (auto& ans : a) {
				cout << setw(5) << right << ans;
			}
			cout << endl;
		}
	}
	return 0;
}

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

int main(){
	static int data_size;
	cin >> data_size;
	while (data_size){
		vector<vector<int>> data;
		vector<vector<int>> data_digit;
		data.resize((data_size + 1)*(data_size + 1));
		data_digit.resize((data_size + 1) * (data_size + 1));
		for (int i = 0; i < data_size; i++){
			data[i].resize(data_size + 1);
			data_digit[i].resize(data_size + 1);
			for (int j = 0; j < data_size; j++){
				cin >> data[i][j];
				int tmp = data[i][j];
				data_digit[i][j] = 0;
				while (tmp){
					tmp /= 10;
					data_digit[i][j]++;
				}
			}
		}
		data[data_size].resize(data_size + 1);
		data_digit[data_size].resize(data_size + 1);
		for (int i = 0; i < data_size; i++){
			int sum = 0;
			for (int j = 0; j < data_size; j++){
				sum += data[i][j];
			}
			data[i][data_size] = sum;
			int tmp = sum;
			data_digit[i][data_size] = 0;
			while (tmp){
				tmp /= 10;
				data_digit[i][data_size]++;
			}

		}
		for (int i = 0; i < data_size + 1; i++){
			int sum = 0;
			for (int j = 0; j < data_size; j++){
				sum += data[j][i];
			}
			data[data_size][i] = sum;
			int tmp = sum;
			data_digit[data_size][i] = 0;
			while (tmp){
				tmp /= 10;
				data_digit[data_size][i]++;
			}
		}

		for (int i = 0; i < data_size + 1; i++){
			for (int j = 0; j < data_size + 1; j++){
				for (int k = data_digit[i][j]; k < 5; k++){
					cout << " ";
				}
				cout << data[i][j];
			}
			if (i!=data_size)
			cout << "\n";
		}

		cin >> data_size;
		if(data_size != 0)cout << "\n";
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime> 

using namespace std;

int main(void){
	int n;
	int a,b;
	int hyo[11][11];
	while(1){
		cin >> n;
		if(n==0)break;
		for(b=0;b<=n;b++){
			for(a=0;a<=n;a++){
				hyo[a][b]=0;
			}
		}
		for(b=0;b<n;b++){
			for(a=0;a<n;a++){
				cin >> hyo[a][b];
				hyo[n][b]+=hyo[a][b];
			}
		}
		for(b=0;b<=n;b++){
			for(a=0;a<n;a++){
				hyo[b][n]+=hyo[b][a];
			}
		}
		for(b=0;b<=n;b++){
			for(a=0;a<=n;a++){
				cout << setw(5) << hyo[a][b];
			}
			cout << endl;
		}
	}
	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<stdio.h>

int a[20][20],i,j,n;

int main(){
	while(scanf("%d",&n)!=EOF){
		if(n==0)break;
		a[n][n]=0;
		for(i=0;i<n;i++){
			a[i][n]=0;
			a[n][i]=0;
			for(j=0;j<n;j++){
				scanf("%d",&a[i][j]);
				a[i][n]+=a[i][j];
			}
			a[n][n]+=a[i][n];
		}
		for(j=0;j<n;j++){
			for(i=0;i<n;i++){
				a[n][j]+=a[i][j];
			}
		}
		for(i=0;i<n+1;i++){
			for(j=0;j<n+1;j++){
				printf("% 5d",a[i][j]);
			}
			if(i!=n)printf("\n");
		}
	}
	
	return 0;
}