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;

int main(){
  int n,sumw,sumh;
  while(1){
    cin>>n;
    if(n==0)break;
    int array[n+1][n+1];
    for(int i=0;i<n;i++){
      sumw=0;
      for(int j=0;j<n;j++){
	cin>>array[i][j];
	sumw+=array[i][j];
      }
      array[i][n]=sumw;
    }
    for(int i=0;i<n+1;i++){
      sumh=0;
      for(int j=0;j<n;j++){
	sumh+=array[j][i];
      }
      array[n][i]=sumh;
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	printf(" %5d",array[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;
    }
}

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

/* {{{ */
#define PI(N) ((M_PI * (N)) / 180) //?§???????????????¢??????????????????
#define CL(N) (cout << (N) << endl)
#define DCT(N) (cout << "debug " << (N) << endl)
#define REP(i, N) for (int i = 0; i < (N); i++)
/* }}} */

int main()
{
    int n;

    while (cin >> n, n){
        int m[11][11], x[11] = {0}, y[11] = {0}, sum = 0;
        REP(i, n){
            REP(j, n){
                cin >> m[i][j];
                x[i] += m[i][j];
                y[j] += m[i][j];
                sum += m[i][j];
            }
        }

        REP(i, n){
            REP(j, n){
                printf("%5d", m[i][j]);
            }
            printf("%5d", x[i]);
            cout << "\n";
        } 

        REP(i, n){
            printf("%5d", y[i]);
        }

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

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

int main()
{
  int n;
  while (cin >> n, n){
    int table[n][n], x[n] = {0}, y[n] = {0}, sum = 0;
    REP(i, n){
      REP(j, n) cin >> table[i][j];
    }
    REP(i, n){
      cout << setw(4) << right << table[i][0];
      x[i] = table[i][0];
      FOR(j, 1, n){
        cout << setw(4) << right << table[i][j];
        x[i] += table[i][j];
      }
      cout << setw(4) << right << x[i] << endl;
    }
    REP(i, n){
      REP(j, n) y[i] += table[j][i];
    }
    cout << setw(4) << right << y[0];
    FOR(i, 1, n) cout << setw(4) << right << y[i];
    REP(i, n) sum += x[i];
    cout << setw(4) << right << sum << endl;
  }
}

C++: #include<stdio.h>
int main()
{
	int a[11][11],n,i,j;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;++i)
		{
			a[i][n] = 0;
			for(j=0;j<n;++j)
			{
				scanf("%d", &a[i][j]);
				a[i][n] += a[i][j];
			}
		}
		for(j = 0; j <= n; ++j)
		{
			a[n][j] = 0;
			for(i = 0; i < n; ++i)
				a[n][j] += a[i][j];
		}
		for(i = 0; i <= n; ++i)
		{
			for(j = 0; j <= n; ++j)
			{
				printf("%5d",a[i][j]);
			}
			putchar('\n');
		}
	}
	return 0;
}

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

int main(){
	
	int n ;
	
	while(cin >> n , n ){
		
		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 <iostream>
#include <vector>
#include <iomanip>
using namespace std;

void calculate(int const n);
void show_table(const vector<int> table, const int n);

int main()
{
	int n = 0;

	while(true) {
		cin >> n;
		if(n==0) break;
		calculate(n);
	}

	return 0;
}

void calculate(int const n)
{
	int row_n = n+1;
	vector<int> table((row_n)*(row_n),0);

	// 表への入力
	for(int i=1;i<=row_n*n;i++) {
		if(i%row_n==0) ;			//  結果が入るところはスキップ
		else
			cin >> table[i-1];
	}

	// 右側の和の計算 
	for(int i=n;i<=(row_n)*(row_n);i+=(row_n))
		for(int j=1;j<=n;j++)
			table[i] += table[i-j];

	// 下側の和の計算
	int count = 0;
	int j = 0;
	for(int i=0;i<n;i++) {
		count = 0;
		j = i;
		while(true) {
			table[(row_n)*n+i] += table[j];
			j += (row_n);
			count++;
			if(count==n) break;
		}
	}
	// 右下の和の計算
	for(int i=n;i<(row_n)*n;i+=(row_n))
		table[(row_n)*(row_n)-1] += table[i];

	show_table(table,n);
}

void show_table(const vector<int> table,const int n)
{
	int row_n = n+1;
	for(int i=0;i<(row_n)*(row_n);i+=(row_n)) {
		for(int j=i;j<i+row_n;j++)
			cout << setw(5) << table[j];
		cout << endl;
	}
}

C++: #include<iostream>
#include<iomanip>
using namespace std;
int main(){
	while(1){
		int n;
		cin>>n;
		if(n==0)break;
		int m[99][99],i,j,k;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				cin>>m[i][j];
		for(i=0;i<n;i++){
			k=0;
			for(j=0;j<n;j++){
				k+=m[i][j];
				if(j+1==n)
					m[i][n]=k;}}
		for(i=0;i<n;i++){
			k=0;
			for(j=0;j<n;j++){
				k+=m[j][i];
				if(j+1==n)
					m[n][i]=k;}}
		k=0;
		for(i=0;i<n;i++)
			k+=m[i][n];
		m[n][n]=k;
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++)
				cout<<setw(5)<<m[i][j];
			cout<<endl;}}
	return 0;}