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<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<ll,ll> P;
const ll MOD=10000;
const ll INF=1000000010;
const ll LINF=1000000000000000010;
const int MAX=100001;
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
ll keta(ll n){
    int d=0;
    while(n>0){
        n/=10;
        d++;
    }
    return d;
}
int main(){
    int n;
    while(cin>>n,n){
        int a[12][12]={};
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>a[i][j];
                a[i][n]+=a[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                a[n][i]+=a[j][i];
            }
        }
        for(int i=0;i<n;i++){
            a[n][n]+=a[n][i];
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                for(int k=keta(a[i][j]);k<=5;k++){
                    cout<<" ";
                }
                cout<<a[i][j];
            }
            cout<<endl;
        }
    }
	return 0;
}

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

using namespace std;

int main(){
	int n[102], 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 <iostream>
#include <cstdio>
using namespace std;

int main() {
	int n;

	while(cin >> n, n) {
		int table[16][16];
		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++) {
			table[i][n] = 0;
			for(int j = 0; j < n; j++) {
				table[i][n] += table[i][j];
			}
		}
		for(int i = 0; i < n + 1; i++) {
			table[n][i] = 0;
			for(int j = 0; j < n; j++) {
				table[n][i] += table[j][i];
			}
		}

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

	return 0;
}

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

using namespace std;

int main() {
	int num;
	while(cin>>num, !cin.eof()) {
		if(num==0) break;
		int table[num][num];
		int col_num[num];

		//initialize
		for(int i=0; i<num; i++){
			for(int j=0; j<num; j++){
				table[i][j] = 0;
			}
			col_num[i] = 0;
		}

		for(int i=0; i<num; i++){
			for(int j=0; j<num; j++){
				cin>>table[i][j];
			}
		}
		int all_sum = 0;
		for(int i=0; i<num; i++){
			int row_sum = 0;
			for(int j=0; j<num; j++){
				cout<< setw(5)<<right <<table[i][j];
				row_sum +=table[i][j];
				col_num[j] += table[i][j];
			}
			cout<< setw(5)<<right<< row_sum<<endl;
			all_sum += row_sum;
		}

		for(int i=0; i<num; i++){
			cout<< setw(5)<<right<< col_num[i];
		}
		cout<< setw(5)<<right <<all_sum<<endl;
	}
	return 0;
}

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

C++: #include <bits/stdc++.h>
using namespace std;

#define int long long

signed main() {
    int n;
    while(cin>>n, n!=0) {
        int G[n+1][n+1] = {0};
        for(int i=0; i<n; ++i) {
            for(int j=0; j<n; ++j) {
                cin >> G[i][j];
                G[i][n]+=G[i][j];
                G[n][j]+=G[i][j];
                G[n][n]+=G[i][j];
            }
        }
        for(int i=0; i<n+1; ++i) {
            for(int j=0; j<n+1; ++j) {
                if(j!=0) cout << " ";
                cout << G[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
using namespace std;
int main(){
	while(1){
	int n; cin >> n ;
	if(!n) break;
	int num[10][10];
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++) cin >> num[i][j];
	for(int i=0;i<n;i++){
		int sum=0;
		for(int j=0;j<n;j++){
			sum+=num[i][j];
			// if(j) cout << ' ' ;
			cout.width(5);
			cout << num[i][j] ;
		};
		cout.width(5);
	cout << sum << '\n' ;
	};
	int* sum=new int[n]; int nsum=0;
	for(int i=0;i<n;i++){
		sum[i]=0;
		// if(i) cout << ' ' ;
		for(int j=0;j<n;j++) sum[i]+=num[j][i];
		cout.width(5);
		cout << sum[i] ;
		nsum+=sum[i];
	};
	cout.width(5);
	cout << nsum << '\n' ;
	};
}

C++: #include <stdio.h>

int main(void)
{
	int n, j, i;
	int mat[15][15];
	
	while(1){
		scanf("%d", &n);
		
		if (n == 0) break;
		
		
		for (i = 0; i <= 14; i++){
			for (j = 0; j <= 14; j++){
				mat[i][j] = 0;
			}
		}
		
		for (i = 0; i < n; i++){
			for (j = 0; j < n; j++){
				scanf("%d", &mat[i][j]);
				mat[i][n] += mat[i][j];
			}
		}
		
		for (i = 0; i <= n; i++){
			for (j = 0; j < n; j++){
				mat[n][i] += mat[i][j];
			}
		}
		
		for (i = 0; i <= n; i++){
			for (j = 0; j <= n; j++){
				printf("%5d", mat[i][j]);
			}
			puts("");
		}
	}
	return (0);
}

C++: #include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int n, t[11][11], i, j, s;
	while (cin >> n && n)
	{
		for (i = 0; i < n; i++)
		{
			s = 0;
			for (j = 0; j < n; j++)
			{
				cin >> t[i][j];
				s += t[i][j];
			}
			t[i][n] = s;
		}
		for (i = 0; i < n; i++)
		{
			s = 0;
			for (j = 0; j < n; j++)
				s += t[j][i];
			t[n][i] = s;
		}
		s = 0;
		for (i = 0; i < n; i++)
		{
			s += t[i][n];
		}
		t[n][n] = s;
		cout << right;
		for (i = 0; i <= n; i++)
		{
			for (j = 0; j <= n; j++)
			{
				cout << setw(5) << t[i][j];
			}
			cout << endl;
		}
	}
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
	int dp[15][15];
	int n;
	while(true){
	cin>>n;
	if(n==0)
		break;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			cin>>dp[i][j];
	int x=0;
	for(int i=0;i<n;i++){
		int h=0;
		for(int j=0;j<n;j++)
			h+=dp[i][j];
		dp[i][n]=h;
		x+=h;
	}
	for(int i=0;i<n;i++){
		int h=0;
		for(int j=0;j<n;j++)
			h+=dp[j][i];
		dp[n][i]=h;
	}
	dp[n][n]=x;
	for(int i=0;i<=n;i++){
		for(int j=0;j<n;j++)
			cout<<dp[i][j]<<" ";
		cout<<dp[i][n]<<endl;
	}
	}
	return 0;
}