#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int MOD = (int)1e9 + 7, BAS = 233;
inline int sign(double x) { return x < -eps ? -1 : x > eps; }
inline int clz(int x) { return __builtin_clz(x); }
inline int clz(long long x) { return __builtin_clzll(x); }
inline int lg2(int x) { return !x ? -1 : 31 - clz(x); }
inline int lg2(long long x) { return !x ? -1 : 63 - clz(x); }
int AP(char *s) {
  unsigned hash = 0;
  for (int i = 0; s[i]; i++) {
    if (i & 1)
      hash ^= (~((hash << 11) ^ s[i] ^ (hash >> 5)));
    else
      hash ^= ((hash << 7) ^ s[i] ^ (hash >> 3));
  }
  return hash & 0x7FFFFFFF;
}
const int M = 500 + 10;
const long long inf = (long long)1e15;
int n;
long long g[M][M];
int x[M];
long long ans[M];
vector<int> a;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%lld", &g[i][j]);
    }
  }
  for (int i = 0; i < n; i++) scanf("%d", x + i);
  for (int cas = n - 1; cas >= 0; cas--) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        g[i][j] = min(g[i][j], g[i][x[cas]] + g[x[cas]][j]);
      }
    }
    a.emplace_back(x[cas]);
    for (int &v : a) {
      for (int &u : a) {
        ans[cas] += g[v][u];
      }
    }
  }
  for (int i = 0; i < n; i++)
    printf(
        "%lld"
        "%c",
        ans[i], " \n"[i == n - 1]);
  return 0;
}
