#include <bits/stdc++.h>
using namespace std;
int o[505], v[505];
int a[505][505];
long long ans[505];
int main() {
  int n;
  long long anss = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) scanf("%d", &a[i][j]);
  for (int i = 0; i < n; i++) scanf("%d", &o[i]), o[i]--;
  for (int i = 0; i < n; i++) v[o[i]] = i;
  for (int ii = n - 1; ii >= 0; ii--) {
    int k = o[ii];
    anss = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        int t = a[i][k] + a[k][j];
        if (t < a[i][j]) a[i][j] = t;
        if (v[i] >= ii && v[j] >= ii) anss += a[i][j];
      }
    ans[ii] = anss;
  }
  for (int i = 0; i < n; i++) printf("%I64d ", ans[i]);
  return 0;
}
