#include <bits/stdc++.h>
using namespace std;
long long edge[501][501];
long long dist[501][501];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  while (t--) {
    long long n;
    cin >> n;
    for (long long i = 1; i < n + 1; i++) {
      for (long long j = 1; j < n + 1; j++) {
        cin >> edge[i][j];
        dist[i][j] = INT_MAX;
      }
    }
    vector<long long> v1;
    for (long long i = 0; i < n; i++) {
      long long num;
      cin >> num;
      v1.push_back(num);
    }
    vector<bool> used(n + 1, false);
    vector<long long> ans(n);
    for (long long i = n - 1; i >= 0; i--) {
      long long res = 0;
      long long vert = v1[i];
      used[vert] = true;
      for (long long j = 1; j < n + 1; j++) {
        for (long long k = 1; k < n + 1; k++) {
          if (j == k) {
            continue;
          }
          dist[j][k] = min(dist[j][k], min(dist[j][vert], edge[j][vert]) +
                                           min(dist[vert][k], edge[vert][k]));
          if (dist[j][k] == INT_MAX) {
            continue;
          }
          if (used[j] && used[k]) {
            res += dist[j][k];
          }
        }
      }
      ans[i] = res;
    }
    for (long long i = 0; i < ans.size(); i++) {
      cout << ans[i] << " ";
    }
  }
  return 0;
}
