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 <iomanip>
#include <vector>

using namespace std;

int main()
{
    int n;
    
    while (true) {
        cin >> n;
        if (!n) {
            break;
        }
        vector<vector<int> > v(n + 1, vector<int>(n + 1, 0));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> v[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                v[i][n] += v[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                v[n][i] += v[j][i];
            }
            v[n][n] += v[n][i];
        }
        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < n + 1; j++) {
                if (j > 0) {
                    cout << ' ';
                }
                cout << setw(5) << v[i][j];
            }
            cout << '\n';
        }
    }
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <climits>
#include <cfloat>

using namespace std;


int table[4096][4096];
int main()
{
	while (true)
	{
		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", &table[i][j]);

		for (int i = 0; i < n; ++i)
		{
			int s = 0;
			for (int j = 0; j < n; ++j)
				s += table[j][i];
			table[n][i] = s;
		}
		for (int i = 0; i <= n; ++i)
		{
			int s = 0;
			for (int j = 0; j < n; ++j)
				s += table[i][j];
			table[i][n] = s;
		}
		for (int i = 0; i <= n; ++i)
		{
			for (int j = 0; j <= n; ++j)
			{
				printf("%5d", table[i][j]);
			}
			printf("\n");
		}
	}

	return 0;
}

C++: //include
//------------------------------------------
#include <bits/stdc++.h> // only for GCC

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

//container util
//------------------------------------------
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

//repetition
//------------------------------------------
#define loop(i,a,b) for(ull i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define riter(i,c) for(auto i=(c).rbegin(); i!=(c).rend(); ++i)

//constant
//--------------------------------------------
const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

// output vector
// ------------------------------------------
namespace std {
    template<typename T>ostream&operator<<(ostream&os, vector<T>const&v){
        iter(it,v) os<<*it<<(it+1==v.end()?"":",");
        return os;
    }
    template<typename T, typename U>ostream&operator<<(ostream&os,const pair<T,U>&p){
        return os << "[" << p.dirst << "," << p.second << "]";
    }
}

//clear memory
// ------------------------------------------
#define clr(a,i) memset((a), (i) ,sizeof(a))

int main(){
   int n,sum;
   while(cin >> n && n){
      int a[n+1][n+1];
      rep(i,n){
         sum=0;
         rep(j,n){
            cin>>a[i][j];
            sum+=a[i][j];
         }
         a[i][n]=sum;
      }
      rep(j,n+1){
         sum=0;
         rep(i,n){
            sum+=a[i][j];
         }
         a[n][j]=sum;
      }
      rep(i,n+1){
         rep(j,n+1){
            printf("%5d",a[i][j]);
         }
         cout << endl;
      }
   }
}

C++: #include<iostream>

using namespace std;

int main()
{
	while(1)
	{
		int data[11][11] = {0};
		int n;
		cin >> n;
		if(n == 0) break;
		for(int y = 0;y <= n;y++)
		{
			for(int x = 0;x <= n;x++)
			{
				if(y < n && x < n) cin >> data[y][x];
				if(x < n) data[y][n] += data[y][x];
				if(y < n) data[n][x] += data[y][x];
				cout.width(5);
				if(x < n || y < n) cout << data[y][x];
				else cout << data[y][x] / 2;
			}
			cout << endl;
		}
	}
	return 0;
}

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

int main(){
  int n;
  while(cin >> n,n){
    vector< vector<int> > v(n,vector<int>(n));
    
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin >> v[i][j];
      }
    }
  
    vector<int> sum(n+1,0);
    for(int i=0;i<n;i++){
      int tmp = 0;
      for(int j=0;j<n;j++){
	tmp += v[i][j];
	sum[j] += v[i][j];
      }
      v[i].push_back(tmp);
      sum[n] += tmp;
    }
    v.push_back(sum);
    
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	printf("% 5d",v[i][j]);
      }
      putchar('\n');
    }
  }
}

C++: #include<stdio.h>

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

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

int t[16][16];

int main()
{
	int n;
	while(cin >> n, n)
	{
		for(int y=0; y<=n; y++)
		for(int x=0; x<=n; x++)
		{
			t[y][x] = 0;
		}

		for(int y=0; y<n; y++)
		for(int x=0; x<n; x++)
		{
			cin >> t[y][x];
			t[n][x] += t[y][x];
			t[y][n] += t[y][x];
			t[n][n] += t[y][x];
		}

		for(int y=0; y<=n; y++)
		{
			for(int x=0; x<=n; x++)
			{
				cout.width(5);
				cout << t[y][x];
			}
			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++) {
			for(j=0;j<n;j++) {
				printf("%5d",hyo[i][j]);
			}
			printf("%5d",wa[i]);
			printf("\n");
		}
		sum=0;
		for(i=0;i<n;i++) {
			printf("%5d",wa2[i]);
			sum+=wa2[i];
		}
		printf("%5d\n",sum);
	}
	return 0;
}