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

int main()
{
	int fig[11][11];
	int n;
	while(cin >> n){
		if(!n)
			break;
		int ans = 0;
		for(int i=0;i<n;i++){
			int sum = 0;
			for(int j=0;j<n;j++){
				cin >> fig[i][j];
				sum += fig[i][j];
			}
			fig[i][n] = sum;
			ans += sum;
		}
		for(int j=0;j<n;j++){
			int sum = 0;
			for(int i=0;i<n;i++){
				sum += fig[i][j];
			}
			fig[n][j] = sum;
		}
		fig[n][n] = ans;
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				cout << setw(5) << fig[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}

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

int main(){
	
	int n ;
	
	while(1){
		cin >> n ;
		if( n==0 ) break ;
		
		int m[11][11] = {} ;
		
		for( int i=0 ; i<n ; i++ ){
			for( int j=0 ; j<n ; j++ ){
				cin >> m[i][j] ;
				m[i][n] += m[i][j] ;
			}
		}
		
		for( int j=0 ; j<n ; j++ ){
			for( int i=0 ; i<n ; i++ ){
				m[n][j] += m[i][j] ;
			}
		}
		
		for( int i=0 ; i<n ; i++ ){
			m[n][n] += m[n][i] ;
		}
		
		for( int i=0 ; i<=n ; i++ ){
			for( int j=0 ; j<=n ; j++ ){
				if( m[i][j] < 100 ) cout << ' ' ;
				cout << "  " << m[i][j] ;
			}
			cout << endl ;
		}
	}
	
return 0 ;
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#define LL long long
using namespace std;
int main(void)
{
	LL n,dist[114][114],i,j,k,sum;
	while(1) {
		cin>>n;
		if(!n)break;
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) {
				cin>>dist[i][j];
				sum+=dist[i][j];
			}
			dist[i][n]=sum;
		}
		for(i=0;i<=n;i++) {
			sum=0;
			for(j=0;j<n;j++) {
				sum+=dist[j][i];
			}
			dist[n][i]=sum;
		}
		for(i=0;i<=n;i++) {
			for(j=0;j<=n;j++) {
				printf("%5d",dist[i][j]);
			}
			cout<<endl;
		}
	}
	return 0;
}

C++: #include <cstdio>

using namespace std;

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

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
#include <set>

#define rep(i,j,k) for(int i=j;i<k;i++)
#define drep(n,m) for(int i=0;i<n;i++)for(int j=0;j<m;j++)
#define Sort(x) sort((x).begin(),(x).end())
#define fi first
#define se second
#define vi vector<int>
#define INF 1000000000
#define MOD 1000000009
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

int main() {
    int n;
    while(1){
        cin>>n;
        if(n==0)return 0;
        vector<vector<int>> v(n+1,vector<int>(n+1,0));
        
        rep(i,0,n){
            int sum=0;
            rep(j,0,n){
                cin>>v[i][j];
                sum+=v[i][j];
            }
            v[i][n]=sum;
        }
        
        rep(j,0,n+1){
            int sum=0;
            rep(i,0,n){
                sum+=v[i][j];
            }
            v[n][j]=sum;
        }
        
        rep(i,0,n+1){
            rep(j,0,n+1){
                if(j==0)printf("%5d",v[i][j]);
                else printf(" %5d",v[i][j]);
            }
            cout<<endl;
        }
        
        
    }
    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#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>
#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;
}