#include <bits/stdc++.h>
using namespace std;
template <class T>
int getbit(int i, T X) {
  return ((X >> (i - 1)) & 1);
}
template <class T>
T onbit(int i, T X) {
  return (X | (1 << (i - 1)));
}
template <class T>
T offbit(int i, T X) {
  return ((X | (1 << (i - 1))) - (1 << (i - 1)));
}
template <class T>
T sqr(T x) {
  return (x * x);
}
template <class T>
T cube(T x) {
  return (x * x * x);
}
template <class T>
T gcd(T a, T b) {
  T r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
int csx[4] = {0, 0, -1, 1};
int csy[4] = {-1, 1, 0, 0};
int n, x[505], dd[505];
long w[505][505];
long long res = 0, ans[505];
void nhap() {
  scanf("%d", &n);
  for (typeof(n) i = 1; i <= n; i++)
    for (typeof(n) j = 1; j <= n; j++) scanf("%ld", &w[i][j]);
  for (typeof(n) i = 1; i <= n; i++) scanf("%d", &x[i]);
}
void xu_ly() {
  memset(dd, 0, sizeof(dd));
  for (typeof(n) k = n; k >= 1; k--) {
    dd[x[k]] = 1;
    for (typeof(n) i = 1; i <= n; i++)
      for (typeof(n) j = 1; j <= n; j++)
        w[i][j] = min(w[i][j], w[i][x[k]] + w[x[k]][j]);
    res = 0;
    for (typeof(n) i = 1; i <= n; i++)
      for (typeof(n) j = 1; j <= n; j++)
        if ((dd[i]) && (dd[j])) res += w[i][j];
    ans[k] = res;
  }
  for (typeof(n) i = 1; i <= n; i++) printf("%I64d ", ans[i]);
}
int main() {
  std::ios::sync_with_stdio(false);
  nhap();
  xu_ly();
  return 0;
}
