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++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

int main(){

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

		int** mat = new int*[n+1];
		for(int i=0;i<n+1;i++) mat[i] = new int[n+1];
		for(int y=0;y<n;y++){
			for(int x=0;x<n;x++){
				int num;
				scanf("%d",&num);
				mat[y][x] = num;
			}
		}

		int sum=0;
		int col=0;
		for(int y=0;y<n;y++){
			for(int x=0;x<n;x++){
				col += mat[y][x];
			}
			mat[y][n]=col;
			sum+=col;
			col=0;
		}

		int row=0;
		for(int x=0;x<n;x++){
			for(int y=0;y<n;y++){
				row += mat[y][x];
			}
			mat[n][x]=row;
			row=0;
		}

		mat[n][n] = sum;

		for(int y=0;y<=n;y++){
			for(int x=0;x<=n;x++){
				printf("%5d%s",mat[y][x],x==n ? "\n" : "");
			}
		}

		for(int i=0;i<n+1;i++) delete[] mat[i];
		delete[] mat;
	}
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <typeinfo>
#include <map>
#define INF 100000000
#define rep(i,a) for(int i=0;i<(a);i++)

using namespace std;
typedef long long ll;



int main(){
    int n;
    while(cin>>n){
        if(n==0)break;
        int data[20][20]={};

        rep(i,n){
            rep(j,n){
                cin>>data[i][j];
            }
        }
        
        rep(i,n){
            int sum=0;
            rep(j,n)sum+=data[i][j];
            data[i][n]=sum;
        }
        
        rep(j,n+1){
            int sum=0;
            rep(i,n)sum+=data[i][j];
            data[n][j]=sum;
        }
        rep(i,n+1){
            rep(j,n+1){
                printf("%4d",data[i][j]);
            }
            cout<<endl;
        }
    }
    return 0;
 }

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
int main(void)
{
	int i,j,hyo[11][11],n,wa[11],wa2[11],sum;
	while(1) {
		scanf("%d",&n);
		if(n==0)  break;
		for(i=0;i<n;i++) {
			for(j=0;j<n;j++) scanf("%d",&hyo[i][j]);
		}
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) sum+=hyo[i][j];
			wa[i]=sum;
		}
		for(i=0;i<n;i++) {
			sum=0;
			for(j=0;j<n;j++) sum+=hyo[j][i];
			wa2[i]=sum;
		}
		for(i=0;i<n;i++) {
			printf(" ");
			for(j=0;j<n;j++) {
				if(hyo[i][j]==100) printf(" %d ",hyo[i][j]);
				if(hyo[i][j]<100 && hyo[i][j]>=10) printf("  %d ",hyo[i][j]);
			}
			printf("%d\n",wa[i]);
			printf("\n");
		}
		printf(" ");
		for(i=0;i<4;i++) {
			if(wa2[i]>=1000) printf("%d ",wa2[i]);
			if(wa2[i]>=100 && wa2[i]<1000) printf(" %d ",wa2[i]);
			else printf("   %d ",wa2[i]);
		}
		sum=0;
		for(i=0;i<4;i++) {
			sum+=wa[i];
		}
		if(sum==1000) printf("%d\n",sum);
		if(sum<1000 && sum>=10) printf("%d\n",sum);
		printf("\n");
	}
	return 0;
}

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

int main(){
	int n,field[11][11];
	while (cin >> n, n){
		for (int i = 0; i < 11; i++){
			for (int j = 0; j < 11; j++) field[i][j] = 0;
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++) cin >> field[i][j];
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++) field[n][i] += field[j][i];
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++) field[i][n] += field[i][j];
		}
		for (int i = 0; i < n; i++) field[n][n] += field[n][i];
		for (int i = 0; i <= n; i++){
			for (int j = 0; j <= n; j++)  printf("%5d",field[i][j]);
			puts("");
		}
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
using namespace std;
int main(void)
{
    int n,A=0,ober[10][10],side[10],lon=0;
    while(1){
        for(int i=0;i<12;i++)side[i]=0;
        cin>>n;
        if(n==0)break;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>ober[i][j];
                side[j]+=ober[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<setw(5)<<ober[i][j]<<" ";
                A+=ober[i][j];
            }
            cout<<setw(5)<<A<<"\n";
            lon+=A;
            A=0;
        }
        for(int i=0;i<n;i++)cout<<setw(5)<<side[i]<<" ";
        cout<<setw(5)<<lon<<"\n";
        lon=0;
        cout<<"\n";
    }
}

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};
int dp[310][310][610];
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++){
                cout<<a[i][j];
                if(j==n){
                    cout<<endl;
                }else{
                    cout<<" ";
                }
            }
        }
    }
	return 0;
}

C++: #include <cstdio>
#include <cstring>
int main(void)
{
  while(1){
    int n;
    int d[111][111];
    scanf("%d",&n);
    if(!n) break;
    memset(d,0,sizeof(d));
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
	scanf("%d",&d[i][j]);
	d[n][j] += d[i][j];
	d[i][n] += d[i][j];
	d[n][n] += d[i][j];
      }
    }
    for(int i = 0; i <= n; i++){
      for(int j = 0; j <= n; j++){
	printf("%5d",d[i][j]);
      }
      puts("");
    }
  }
}

C++: #include<iostream>
#include<vector>
int main()
{
	while (1) {
		int sum = 0;
		int num;
		std::cin >> num;
		if (num == 0)break;
		std::vector<std::vector<int>>vec(num + 1, std::vector<int>(num + 1));
		for (int i = 0; i < num; ++i) {
			for (int j = 0; j < num; ++j) {
				std::cin >> vec[i][j];
			}
		}
		for (int i = 0; i < num; ++i) {
			for (int j = 0; j < num; ++j) {
				vec[num][i] += vec[j][i];
				sum += vec[j][i];
				vec[i][num] += vec[i][j];
			}
		}
		vec[num][num] = sum;
		for (int i = 0; i < num + 1; ++i) {
			for (int j = 0; j < num + 1; ++j) {
				if (j == num)std::cout << vec[i][j] << std::endl;
				else std::cout << vec[i][j] << " ";
			}
		}
	}
	return 0;
}

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

int main(){

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