#include <bits/stdc++.h>
using namespace std;
long long G[333][333];
long long min(long long a, long long b, long long c) {
  return min(a, min(b, c));
}
int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) cin >> G[i][j];
  int M;
  cin >> M;
  while (M--) {
    long long u, v, x;
    cin >> u >> v >> x;
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++) {
        G[i][j] = min(G[i][j], G[i][u] + G[v][j] + x, G[i][v] + G[u][j] + x);
      }
    long long sum = 0;
    for (int i = 1; i <= N; i++)
      for (int j = i; j <= N; j++) sum += G[i][j];
    cout << sum << ' ';
  }
}
