code:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//#define DEBUG
typedef long long LL;
int n,a[10][10],x,y,z,dp[10][10][10][10];
int main(){
cin >> n;
cin >> x >> y >> z;
while(x && y && z){
a[x][y] = z;
cin >> x >> y >> z;
}
#ifdef DEBUG
for(int i = 1;i <= n; i++){
for(int j = 1;j <= n; j++) cout << a[i][j] << " ";
cout << endl;
}
#endif
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
for(int k = 1;k <= n; k++)
for(int l = 1;l <= n; l++){
dp[i][j][k][l] = max(max(dp[i-1][j][k][l],dp[i][j-1][k][l]),max(dp[i][j][k-1][l],dp[i][j][k][l-1])) + a[i][j] + a[k][l];
if(i == k && j == l) dp[i][j][k][l] -= a[i][j];
}
cout << dp[n][n][n][n];
return 0;
}