#include <bits/stdc++.h>
template <typename Tp>
Tp Max(const Tp &a, const Tp &b) {
  return a > b ? a : b;
}
template <typename Tp>
Tp Min(const Tp &a, const Tp &b) {
  return a < b ? a : b;
}
template <typename Tp>
void Read(Tp &x) {
  Tp in = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    in = in * 10 + ch - '0';
    ch = getchar();
  }
  x = in * f;
}
const int SN = 500 + 10;
long long f[SN][SN], E[SN][SN], pos[SN], res[SN], n;
int cx[SN];
int main() {
  Read(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) Read(f[i][j]);
  for (int i = 1; i <= n; i++) Read(pos[i]), cx[pos[i]] = i;
  for (int k = n; k >= 1; k--) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        f[i][j] = Min(f[i][j], f[i][pos[k]] + f[pos[k]][j]);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (cx[i] >= k && cx[j] >= k) ans += f[i][j];
    res[k] = ans;
  }
  for (int i = 1; i <= n; i++) printf("%I64d ", res[i]);
  putchar(10);
  return 0;
}
