#include <bits/stdc++.h>
using namespace std;
long long d[309][309];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> d[i][j];
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++) {
        d[j][k] = min(d[j][k], d[j][x] + d[y][k] + z);
        d[j][k] = min(d[j][k], d[j][y] + d[x][k] + z);
      }
    long long sum = 0;
    for (int j = 1; j <= n; j++)
      for (int k = j; k <= n; k++) sum += d[j][k];
    cout << sum << " ";
  }
}
