#include <bits/stdc++.h>
using namespace std;
long long n, ary[310][310], k;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> ary[i][j];
  cin >> k;
  for (long long i = 1; i <= k; i++) {
    long long u, v, wt, ans = 0;
    cin >> u >> v >> wt;
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++) {
        ary[i][j] = min(ary[i][j], ary[i][u] + ary[v][j] + wt);
        ary[i][j] = min(ary[i][j], ary[i][v] + ary[u][j] + wt);
      }
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++)
        if (i < j) ans += ary[i][j];
    cout << ans << " ";
  }
  cout << "\n";
  return 0;
}
