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>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;


signed main(){
  int n;
  int M[12][12];
  while(cin>>n,n){
    rep(i,n)rep(j,n)cin>>M[i][j];
    cout<<endl;
    rep(i,n){
      int tmp=0;
      rep(j,n)tmp+=M[i][j];
      M[i][n]=tmp;
    }
    rep(i,n){
      int tmp=0;
      rep(j,n)tmp+=M[j][i];
      M[n][i]=tmp;
    }
    int tmp=0;
    rep(i,n)tmp+=M[n][i];
    M[n][n]=tmp;
    rep(i,n+1){
      rep(j,n+1){
        cout<<M[i][j];
        if(j<n+1)cout<<' ';
      }
      cout<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

int matrix[11][11];

int main()
{
	int n,sum;
	
	for(;;)
	{
		cin>>n;
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin>>matrix[i][j];
			}
		}

		for(int i=0;i<n;i++)
		{
			sum=0;
			for(int j=0;j<n;j++)
			{
				sum+=matrix[i][j];
			}
			matrix[i][n]=sum;
		}
		for(int j=0;j<n;j++)
		{
			sum=0;
			for(int i=0;i<n;i++)
			{
				sum+=matrix[i][j];
			}
			matrix[n][j]=sum;
		}
		sum=0;
		for(int i=0;i<n;i++)
		{
			sum+=matrix[i][n];
		}
		matrix[n][n]=sum;
		
		for(int i=0;i<=n;i++)
		{
			for(int j=0;j<=n;j++)
			{
				if(j>0)cout<<' ';
				cout<<matrix[i][j];
			}
			cout<<endl;
		}
	}
}

C++: 
#include <iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int main(){
    int a[10][10],n,i,j,tate[10]={0},yoko[10]={0},sum=0;
    while(cin>>n){
        if(n==0)break;
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                cin>>a[i][j];
                sum+=a[i][j];
                yoko[i]+=a[i][j];
                tate[j]+=a[i][j];
            }
        }
        for(i=0;i<n;i++){
            for(j=0;j<=n;j++){
                if(j==n)printf("%5d\n",yoko[i]);
                else printf("%5d",a[i][j]);
            }
        }
        for(j=0;j<=n;j++){
            if(j!=n)printf("%5d",tate[j]);
            else if(j==n)printf("%5d\n",sum);
        }
        for(i=0;i<10;i++){
            tate[i]=0;
            yoko[i]=0;
        }
        sum=0;
    }
     return 0;
}

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

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

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

#define repeat(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int table[11][11];

int main() {
  for (;;) {
    int n;
    scanf("%i", &n);
    if (!n) return 0;
    memset(table, 0, sizeof table);
    repeat(i, n) {
      repeat(j, n) {
        scanf("%i", &table[i][j]);
        table[i][n] += table[i][j];
        table[n][j] += table[i][j];
        printf("%5i", table[i][j]);
      }
      printf("%5i\n", table[i][n]);
    }
    repeat(i, n)
      table[n][n] += table[n][i];
    repeat (i, n+1)
      printf("%5i", table[n][i]);
    puts("");
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int graph[128][128];
	int num;
	
	while(cin >> num)
	{
		if(!num) break;
		int count = 0;
		int all = 0, las[128];
		for(int i = 0; i < 128; i++)
			las[i] = 0;

		for(int i = 0; i < num; i++)
		{
			for(int j = 0; j < num; j++)
			{
				cin >> graph[i][j];
				count += graph[i][j];
				las[j]  += graph[i][j];
				all += graph[i][j];
				if(j == num - 1) 
				{
					graph[i][j+1] = count;
					count = 0;
				}
				if(i == num - 1)
				{
					graph[i+1][j] = las[j];
				}
				if(i == num - 1 && j == num - 1)
				{
					graph[i+1][j+1] = all;
				}
			}
		}

		for(int i = 0; i < num + 1; i++)
		{
			for(int j = 0; j < num + 1; j++)
			{
				printf("%5d", graph[i][j]);
				if(j != num)
					printf(" ");
			}
			puts("");
		}

	}
}

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

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) {
                printf("%5d", G[i][j]);
            }
            cout << endl;
        }
    }
    return 0;
}

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

int main(void) {

  int size;
  int matrix[10][10];

  int sum_row,sum_column,sum;
  char line[200];
  char* p;

  int i,j;
  char c;

  
  while(1) {

    scanf("%d",&size);
    c=getchar();

    if(size==0) break;

    for(i=0;i<size;i++) {

      fgets(line,sizeof(line),stdin);
      line[strlen(line)-1]='\0';

      p=strtok(line," ");
      matrix[i][0]=atoi(p);

      for(j=1;j<size;j++) {
	p=strtok(NULL," ");
	matrix[i][j]=atoi(p);
      }

    }

    for(i=0;i<size;i++) {
     
      sum_row=0;

      for(j=0;j<size;j++) {

	printf("%5d",matrix[i][j]);

	sum_row+=matrix[i][j];

      }

      printf("%5d\n",sum_row);
    }

    sum=0;

    for(j=0;j<size;j++) {

      sum_column=0;

      for(i=0;i<size;i++) {

	sum_column+=matrix[i][j];

      }

      sum+=sum_column;

      printf("%5d",sum_column);
    }

    printf("%5d\n",sum);

  }

  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";
}