Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}

		input := make([][]int, n+1, n+1)
		totalRow := make([]int, n+1, n+1)

		for i := 0; i < n; i++ {
			row := make([]int, n+1, n+1)
			scanner.Scan()

			row = numbers(scanner.Text())

			input[i] = row
			total(totalRow, row)
		}

		input[n] = totalRow

		for xk := range input {
			for yk, yv := range input[xk] {
				if yk == n {
					fmt.Printf("%5d\n", yv)
				} else {
					fmt.Printf("%5d", yv)
				}
			}
		}
	}
}

func total(sum, row []int) {
	for i := 0; i < len(row); i++ {
		sum[i] += row[i]
	}
}

func numbers(s string) []int {
	var n []int
	var sum int
	for _, f := range strings.Fields(s) {
		i, err := strconv.Atoi(f)
		if err == nil {
			n = append(n, i)
			sum += i
		}
	}
	n = append(n, sum)
	return n
}

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

using namespace std;

int main()
{
	int n;
	
	while (scanf("%d", &n), n != 0){
		int t[11][11] = {0};
		
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> t[i][j];
				t[i][n] += t[i][j];
				t[n][j] += t[i][j];
			}
			t[n][n] += t[i][n];
		}
		for (int i = 0; i <= n; i++){
			for (int j = 0; j <= n; j++){
				printf("%5d", t[i][j]);
			}
			printf("\n");
		}
	}
	return (0);
}

C++: #include<bits/stdc++.h>
using namespace std;
const int N=1000;
int n,a[N][N];
signed main(void){
	ios::sync_with_stdio(false);
	while(cin>>n&&n){
		memset(a,0,sizeof(a));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				cin>>a[i][j];
		for(int i=1,sum;i<=n;i++){
			sum=0;
			for(int j=1;j<=n;j++)
				sum+=a[i][j];
			a[i][n+1]=sum;
		}
		for(int j=1,sum;j<=n;j++){
			sum=0;
			for(int i=1;i<=n;i++)
				sum+=a[i][j];
			a[n+1][j]=sum;
		}
		int sum=0;
		for(int i=1;i<=n;i++)
			 sum+=a[i][n+1];
		a[n+1][n+1]=sum;
		for(int i=1;i<=n+1;i++,cout<<endl)
			for(int j=1;j<=n+1;j++)
				cout<<setw(5)<<a[i][j];
	}
}

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

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

int main(){
	int n;
	while(1){
		cin >> n;
		if(n==0) break;
		int a[n+1][n+1];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++) cin >> a[j][i];
			a[n][i] = 0;
			for(int j=0;j<n;j++) a[n][i] += a[j][i];
		}
		for(int i=0;i<=n;i++){
			a[i][n] = 0;
			for(int j=0;j<n;j++) a[i][n] += a[i][j];
		}
		for(int i=0;i<=n;i++){
			cout << right << setw(5) << a[0][i];
			for(int j=1;j<=n;j++) cout << right << setw(5) << a[j][i];
			cout << endl;
		}
	}
}

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

int main(){
  int n,w,h,sup[10][10],wt[10],ht[10],at;
	
	while(1){
		w=0;
		h=0;
		at=0;
		
		cin >> n;
		
		if(n==0) break;
		
		for(h=0; h<n; h++){

			wt[h] = 0;

			for(w=0; w<n; w++){
				if(h==0) ht[w]=0;

				sup[h][w] = 0;

				cin >> sup[h][w];
				
				wt[h] += sup[h][w];
				
				ht[w] += sup[h][w];
			}
		}
		
		for(h=0; h<=n; h++){
			for(w=0; w<=n; w++){
				if(h<n && w<n){
					printf("%5d",sup[h][w]);
					
					at += sup[h][w];
				}
				
				else if(w==n && h<n){
					printf("%5d\n",wt[h]);
				}
				
				else if(h==n && w<n){
					printf("%5d",ht[w]);
				}
				
				else{
					printf("%5d\n",at);
				}
			}
		}
		
	}
  
  return 0;
}

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

int main(){
  int n;
  while(cin>>n){
    int a[11][11],b[10]={},c[10]={},d=0;
    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++){
	b[i]+=a[i][j];
	c[i]+=a[j][i];
      }
    for(int i=0;i<n;i++)
      d+=b[i];
    for(int i=0;i<n+1;i++){
      a[i][n]=b[i];
      a[n][i]=c[i];
      a[n][n]=d;
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++)
	cout<<a[i][j]<<' ';
      cout<<endl;
    }
  }
  return 0;
}

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

int main() {
  int field[100][100];
  int n,x;
  while (cin >> n) {
    if(n == 0) break;
    int field[100][100] = {0};
    for(int i=0;i<n;i++) {
      for(int j=0;j<n;j++) {
        cin >> x;
        field[i][j] = x;
      }
    }
    for(int i=0;i<n;i++) {
      for(int j=0;j<n;j++) {
        field[i][n] += field[i][j];
      }
    }
    for(int i=0;i<n;i++) {
      for(int j=0;j<n;j++) {
        field[n][i] += field[j][i];
      }
    }
    for(int i=0;i<n;i++) {
      field[n][n] += field[i][n];
    }
    for(int i=0;i<n+1;i++) {
      for(int j=0;j<n+1;j++) {
        cout << field[i][j] << " ";
      }
      cout << "\b" << endl;
    }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
int d[11][11];
int main()
{
	while(1)
	{
		int n;
		cin >> n;
		if(n==0)break;
		for(int i=0;i<11;i++)
		{
			for(int j=0;j<11;j++)
			{
				d[i][j]=0;
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin >> d[i][j];
			}
		}
		int x=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				d[i][n]+=d[i][j];
				d[n][i]+=d[j][i];
				x+=d[i][j];
			}
		}
		d[n][n]=x;
		for(int i=0;i<=n;i++)
		{
			for(int j=0;j<=n;j++)
			{
				printf("%5d",d[i][j]);
			}
			cout << endl;
		}
	}
	return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int a[53][53];

int main()
{
    int n;
    while (cin >> n, n)
    {
        int r_sum[n] = {}, c_sum[n] = {}, sum = 0;
        rep(i, n)
        {
            rep(j, n)
            {
                cin >> a[i][j];
                r_sum[i] += a[i][j];
                c_sum[j] += a[i][j];
                sum += a[i][j];
            }
        }
        r_sum[n] = sum;

        rep(j, n) a[n][j] = c_sum[j];

        rep(i, n + 1)
        {
            rep(j, n)
            {
                cout << right << setw(5) << a[i][j];
            }
            cout << right << setw(5) << r_sum[i] << endl;
        }
    }
    return 0;
}