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 <cstdio>

using namespace std;

int main(void){

  int n;
  int a[10][10];
  
  while( cin >> n , n ){
    
    for( int i = 0 ; i < n ; i++ )
      for( int j = 0 ; j < n ; j++ )
	cin >> a[i][j];
    
    for( int i = 0 ; i < n ; i++ ){
      for( int j = 0 ; j < n ; j++ ){
	printf("%5d", a[i][j]);
	
	if( j == n - 1 ){
	  int sum = 0;
	  for( int k = 0 ; k < n ; k++ ){
	    sum += a[i][k];
	  }
	  printf("%5d", sum);
	}
      }
      cout << endl;
    }
    
    for( int i = 0 ; i < n ; i++ ){
      int sum = 0;
      for( int j = 0 ; j < n ; j++ ){
	sum += a[j][i];
      }
      printf("%5d", sum);
    }
    
    int sum = 0;
    for( int i = 0 ; i < n ; i++ ){
      for( int j = 0 ; j < n ; j++ ){
	sum += a[i][j];
      }
    }
    printf("%5d", sum);
    cout << endl;
  }
  return 0;
}

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

using namespace std;

int main()
{
    int n;

    while (cin >> n) {
        if (!n)
            break;

        int tmp;
        int mat[11][11];

        for (int i = 0; i < 11; i++)
            for (int j = 0; j < 11; j++)
                mat[i][j] = 0;


        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> tmp;
                mat[i][j] = tmp;
                mat[i][n] += tmp;
                mat[n][j] += tmp;
            }
        }

        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                if (j == n && i != n)
                    mat[n][n] += mat[i][j];
                printf("%5d", mat[i][j]);
            }
            putchar('\n');
        }
    }
    
    return 0;
}

C++: #include <stdio.h>

int main(void){
	int matrix[100][100],i,j,dataset;
	scanf("%d",&dataset);
	while(dataset!=0){

		//init add total norm
		for(i=0;i<dataset;i++){
			matrix[dataset][i]=0;
			matrix[i][dataset]=0;
		}

		//input and calc
		for(i=0;i<dataset;i++){
			for(j=0;j<dataset;j++){
				scanf("%d",&matrix[i][j]);
				matrix[i][dataset]+=matrix[i][j];
			}
		}

		//calc
		for(j=0;j<dataset+1;j++){
			for(i=0;i<dataset;i++){
				matrix[dataset][j]+=matrix[i][j];
			}
		}

		//output
                for(i=0;i<dataset+1;i++){
                        for(j=0;j<dataset+1;j++){
                                printf("%5d",matrix[i][j]);
                        }
			printf("\n");
                }
	scanf("%d",&dataset);
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n,c;
  while(cin>>n,n){
    int a[n+1][n+1];
    rep(i,n){c=0;rep(j,n){
        cin>>a[i][j];
        c+=a[i][j];
      }
      a[i][n]=c;
    }
    rep(j,n){c=0;rep(i,n){
      c+=a[i][j];
    }
    a[n][j]=c;
  }
  rep(i,n+1){rep(j,n+1){cout<<a[i][j];}cout<<endl;}
  }
}

C++: #include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n,sum=0,sumy=0;
	while (1){
		sum = 0;
		sumy = 0;
		cin >> n;
		if (n == 0)break;
		int ma[101][101] = { 0 };
		for (int i = 0; i < n; i++){
			sum = 0;
			for (int i2 = 0; i2 < n; i2++){
				cin >> ma[i][i2];
				sum += ma[i][i2];

			}
			sumy += sum;
			ma[i][n] += sum;
		}
		ma[n][n] += sumy;
		for (int i = 0; i < n; i++){
			sum = 0;
			for (int i2 = 0; i2 < n; i2++){
				sum += ma[i2][i];
				sumy += sum;
			}
			ma[n][i] += sum;
		}
		n++;
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				printf("%5d", ma[i][i2]);
			}
			cout << endl;
		}
	}
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-9

const int MAX_N = 10;
int n;
int a[MAX_N+1][MAX_N+1];

int main()
{
  while((cin >> n) && n){
    rep(i, n+1) rep(j, n+1) a[i][j] = 0;
    rep(i, n)   rep(j, n)   cin >> a[i][j];

    rep(i, n)   rep(j, n)   a[n][j] += a[i][j];
    rep(i, n+1) rep(j, n)   a[i][n] += a[i][j];

    rep(i, n+1) rep(j, n+1) printf("%5d%s", a[i][j], (j==n ? "\n" : ""));
  }

  return 0;
}

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 <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], y[n], sum = 0;
    REP(i, n){
      REP(j, n) cin >> table[i][j];
      x[i] = y[i] = 0;
    }
    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 "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		vector<vector<int>>dates(N + 1, vector<int>(N + 1));
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				cin >> dates[i][j];
				dates[N][j] += dates[i][j];
				dates[i][N] += dates[i][j];
				dates[N][N] += dates[i][j];
			}
		}
		for (int i = 0; i <= N; ++i) {
			for (int j = 0; j <= N; ++j) {
				printf("%5d", dates[i][j]);
			}
			printf("\n");
		}
	}
	
	return 0;
}