#include <bits/stdc++.h>
using namespace std;
struct __s {
  __s() {
    srand(time(NULL));
    if (1) {
      ios_base::Init i;
      cin.sync_with_stdio(0);
      cin.tie(0);
    }
  }
  ~__s() {
    if (!1)
      fprintf(stderr, "Execution time: %.3lf s.\n",
              (double)clock() / CLOCKS_PER_SEC);
    long long n;
    cin >> n;
  }
} __S;
long long d[333][333];
int main(void) {
  long long n;
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < (long long)(n); i++) {
    for (long long j = 0; j < (long long)(n); j++) {
      cin >> d[i][j];
      sum += d[i][j];
    }
  }
  sum /= 2;
  long long m;
  cin >> m;
  for (long long _ = 0; _ < (long long)(m); _++) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    for (long long i = 0; i < (long long)(n); i++) {
      for (long long j = i + 1; j < n; j++) {
        long long res1 = d[i][a] + c + d[b][j];
        long long res2 = d[i][b] + c + d[a][j];
        long long res = min(res1, res2);
        if (d[i][j] > res) {
          sum -= d[i][j] - res;
          d[i][j] = d[j][i] = res;
        }
      }
    }
    cout << sum << " ";
  }
  cout << '\n';
  return 0;
}
