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;
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;
}

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

#define rep(i, n) for(int i = 0; i < n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
 
using namespace std; 

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
 
int main(void){
    for(int n; cin >> n, n;){
        vector<vi> res(n + 1);
        rep(y, n + 1) res[y].resize(n + 1);
        rep(y, n) rep(x, n) cin >> res[y][x];

        rep(y, n) rep(x, n) res[y][n] += res[y][x], res[n][x] += res[y][x];
        rep(y, n) res[n][n] += res[y][n];

        rep(y, n + 1){ rep(x, n + 1) printf("%5d", res[y][x]); cout << endl; }
    }
 
    return 0;
}