#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
long long n, f[505][505], x[505], res[505];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> f[i][j];
  for (long long i = 1; i <= n; i++) cin >> x[i];
  reverse(x + 1, x + n + 1);
  for (long long k = 1; k <= n; k++) {
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++)
        f[i][j] = min(f[i][j], f[i][x[k]] + f[x[k]][j]);
    long long sum = 0;
    for (long long i = 1; i <= k; i++)
      for (long long j = 1; j <= k; j++) sum += f[x[i]][x[j]];
    res[k] = sum;
  }
  for (long long i = n; i >= 1; i--) cout << res[i] << ' ';
  cout << endl;
  return 0;
}
