#include <bits/stdc++.h>
using namespace std;
long long const inf = 0x3f3f3f3f3f3f3f3f;
long long n;
long long d[510][510], ord[510], ans[510];
bool vis[510];
signed main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) scanf("%I64d", &d[i][j]);
  }
  for (long long i = 1; i <= n; i++) scanf("%I64d", &ord[i]);
  for (long long k = n; k >= 1; k--) {
    vis[ord[k]] = 1;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (i != j) d[i][j] = min(d[i][j], d[i][ord[k]] + d[ord[k]][j]);
      }
    }
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (vis[i] && vis[j]) ans[k] += d[i][j];
      }
    }
  }
  for (long long i = 1; i <= n; i++) printf("%I64d ", ans[i]);
  printf("\n");
  return 0;
}
