#include <bits/stdc++.h>
using namespace std;
long long gcd1(long long a, long long b) {
  if (a == 0) return b;
  return gcd1(b % a, a);
}
long long modx(long long base, long long ex) {
  long long ans = 1LL, val = base;
  while (ex > 0LL) {
    if (ex & 1LL) ans = (ans * val) % 1000000009LL;
    val = (val * val) % 1000000009LL;
    ex = ex >> 1LL;
  }
  return ans;
}
const int maxn = 505;
int dist[maxn][maxn];
int a[maxn], n;
vector<int> v;
vector<long long> ans(maxn);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cin >> dist[i][j];
  }
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int k = n; k >= 1; k--) {
    int K = a[k];
    long long sum = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        int pt1 = v[i];
        int pt2 = v[j];
        dist[K][pt2] = min(dist[K][pt2], dist[K][pt1] + dist[pt1][pt2]);
        dist[pt2][K] = min(dist[pt2][K], dist[pt2][pt1] + dist[pt1][K]);
      }
    }
    v.push_back(K);
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        int pt1 = v[i];
        int pt2 = v[j];
        dist[pt1][pt2] = min(dist[pt1][pt2], dist[pt1][K] + dist[K][pt2]);
        sum += dist[pt1][pt2];
      }
    }
    ans[k - 1] = (sum);
  }
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
