#include <bits/stdc++.h>
const unsigned long long inf = 1LL << 61;
const double eps = 1e-5;
using namespace std;
bool cmp(int a, int b) { return a > b; }
int dis[310][310];
int main() {
  int n, k;
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      scanf("%d", &dis[i][j]);
    }
  cin >> k;
  int a, b, c;
  while (k--) {
    scanf("%d%d%d", &a, &b, &c);
    if (c < dis[a][b]) {
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          dis[i][j] = dis[j][i] = min(dis[i][j], dis[i][a] + dis[b][j] + c);
          dis[i][j] = dis[j][i] = min(dis[i][j], dis[i][b] + dis[a][j] + c);
        }
    }
    long long sum = 0;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) {
        sum += dis[i][j];
      }
    cout << sum << " ";
  }
  return 0;
}
