#include <bits/stdc++.h>
using namespace std;
long long G[500][500], d[500][500];
long long x[500], n, m;
void floyd(long long k) {
  for (long long i = 0; i < n; i++)
    if (i != k) {
      for (long long j = 0; j < n; j++)
        if (i != j && j != k)
          if (G[i][j] > G[i][k] + G[k][j]) {
            G[i][j] = G[i][k] + G[k][j];
          }
    }
}
void build_graph() {
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      scanf("%I64d", &G[i][j]);
    }
  }
  for (long long i = 0; i < n; i++) {
    scanf("%I64d", &x[n - i - 1]);
    x[n - i - 1]--;
  }
}
void solve() {
  m = 0;
  long long ans[500] = {0};
  memcpy(d, G, sizeof(G));
  for (long long i = 0; i < n; i++) {
    m++;
    floyd(x[i]);
    for (long long j = 0; j < m; j++) {
      for (long long k = 0; k < m; k++) {
        ans[n - i - 1] += G[x[j]][x[k]];
      }
    }
  }
  printf("%I64d", ans[0]);
  for (long long i = 1; i < n; i++) printf(" %I64d", ans[i]);
  putchar(10);
}
int main() {
  while (~scanf("%I64d", &n)) {
    build_graph();
    solve();
  }
  return 0;
}
