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 <cstdio>
#include <vector>
using namespace std;

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

    vector<vector<int> > table(n+1, vector<int>(n+1));
    for (int i = 0; i < n; ++i) {
      int sum = 0;
      for (int j = 0; j < n; ++j) {
	scanf("%d", &table[i][j]);
	sum += table[i][j];
      }
      table[i][n] = sum;
    }

    for (int i = 0; i < n+1; ++i) {
      int sum = 0;
      for (int j = 0; j < n; ++j)
	sum += table[j][i];
      table[n][i] = sum;
    }

    for (int i = 0; i < n+1; ++i) {
      for (int j = 0; j < n+1; ++j)
	printf("%5d", table[i][j]);
      puts("");
    }
  }
  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 <cstdio>
using namespace std;

int n;
int field[1000][1000];
int total;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) return 0;

    total = 0;
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
        scanf("%d", &field[i][j]);
        total += field[i][j];
      }
    }

    for (int i=0; i<n; i++) {
      int sum = 0;
      for (int j=0; j<n; j++) {
        sum += field[i][j];
      }
      field[i][n] = sum;
    }
    field[n][n] = total;

    for (int j=0; j<n; j++) {
      int sum = 0;
      for (int i=0; i<n; i++) {
        sum += field[i][j];
      }
      field[n][j] = sum;
    }

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

C++: #include <bits/stdc++.h>

#define loop(v, f, l) for(int v = (f), v##_ = (l); v < v##_; ++v)

using namespace std;

typedef long long int lint;
static inline int in(){ int x; scanf("%d", &x); return (x); }
static inline lint inl(){ lint x; scanf("%lld", &x); return (x); }

int matrix[11][11];

int main()
{
	int n;

	while( (n = in()) != 0 )
	{
		loop(i, 0, 11)
		{
			loop(j, 0, 11)
			{
				matrix[i][j] = 0;
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][j] = in();
			}
		}
		
		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][n] += matrix[i][j];
				matrix[n][i] += matrix[j][i];
			}
			matrix[n][n] += matrix[i][n];
		}
		
		loop(i, 0, n + 1)
		{
			loop(j, 0, n + 1)
			{
				printf("%5d", matrix[i][j]);
			}
			printf("\n");
		}
		
	}

	return (0);
}

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

int main(int argc, char *argv[]){
  int n;
  while(cin >> n,n){
    int mat[n + 1][n + 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> mat[i][j];
      }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int hor = 0;
      int var = 0;
      for (int j = 0; j < n; j++) {
        hor += mat[i][j];
        var += mat[j][i];
      }
      mat[i][n] = hor;
      mat[n][i] = var;
      sum += hor;
    }
    mat[n][n] = sum;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        printf("%5d ", mat[i][j]);
      }
      std::cout << std::endl;
    }
  }
  return 0;
}

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

int hoge(int w);
int getKetasu(int n);

int main(){
	int w;
	
	while(1){
	 cin>>w;
	 if(w==0){break;}
	 hoge(w);
	}
	
	return 0;
}

int hoge(int w){	//?????£???????????????
	int t[10][10];
	int ketasu;
	int x,y,i;
	
	for(y=0;y<w;y++){
	 for(x=0;x<w;x++){
	  cin>>t[x][y];
	 }
	}
	
	for(y=0;y<w;y++){
	 t[w][y]=0;
	 for(x=0;x<w;x++){
	  t[w][y]+=t[x][y];
	 }
	}
	for(x=0;x<=w;x++){
	 t[x][w]=0;
	 for(y=0;y<w;y++){
	  t[x][w]+=t[x][y];
	 }
	}
	
	for(y=0;y<=w;y++){
	 for(x=0;x<=w;x++){
	  cout<<" ";
	  ketasu=getKetasu(t[x][w]);
	  for(i=getKetasu(t[x][y]);i<ketasu;i++){cout<<" ";}
	  cout<<t[x][y];
	 }
	 cout<<endl;
	}
	
	return 0;
}

int getKetasu(int n){	//?????°?????????
	if(n<10){
	 return 1;
	}else if(n<100){
	 return 2;
	}else if(n<1000){
	 return 3;
	}else{
	 return 4;
	}
}

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

int main(void)
{
  int n;
  int i, j;
  while(1){
    cin >> n;
    if(n == 0){
      break;
    }
    
    
    int array[11][11] = {};
    for(i = 0; i < n; i++){
      for(j = 0; j < n; j++){
	cin >> array[i][j];
	array[i][n] = array[i][n] + array[i][j];
	array[n][j] = array[n][j] + array[i][j];
	array[n][n] = array[n][n] + array[i][j];
      }
    }

    for(i = 0; i <= n; i++){
      for(j = 0; j <= n; j++){
	std::cout << std::right << std::setw(4) << array[i][j];
      }
      cout << endl;
    } 

  }

    
    
  return 0;
}

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

using namespace std;

int main()
{
	int n;
	int i,j;
	for(;;)
	{
		cin >> n ;
		if(n==0)
			break;
		int table[12][12]={0};
		
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				cin >> table[i][j] ;
			}
		}
		
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				if(j!=n)
				{
					printf("%5d",table[i][j]);
					table[i][n]+=table[i][j];
					table[n][j]+=table[i][j];
				}
				else
					printf("%5d\n",table[i][j]);
			}
		}
	}
}

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