#include <bits/stdc++.h>
using namespace std;
const int MAX = 309;
int dis[MAX][MAX], n, k;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> dis[i][j];
  cin >> k;
  for (int i = 0, v, u, w; i < k; i++) {
    cin >> v >> u >> w, v--, u--;
    long long ans = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        dis[i][j] = min(dis[i][j], min(dis[i][v] + w + dis[u][j],
                                       dis[i][u] + w + dis[j][v])),
        ans += dis[i][j];
    cout << ans / 2 << " ";
  }
}
