#include <bits/stdc++.h>
using namespace std;
long long int a[100000 + 1], spf[100000 + 1];
long long int const MAXN = 100001;
long long int modex(long long int x, long long int y, long long int p);
void getZarr(string str, long long int Z[]);
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++) spf[i] = i;
  for (int i = 4; i < MAXN; i += 2) spf[i] = 2;
  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j) spf[j] = i;
    }
  }
}
vector<long long int> getFactorization(long long int x) {
  vector<long long int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n;
  cin >> n;
  long long int a[n + 1][n + 1];
  for (long long int i = 1; i < n + 1; i++) {
    for (long long int j = 1; j < n + 1; j++) cin >> a[i][j];
  }
  long long int b[n];
  for (long long int i = 0; i < n; i++) cin >> b[i];
  long long int occ[100000 + 1] = {0};
  map<long long int, long long int> m;
  for (long long int k = n - 1; k >= 0; k--) {
    for (long long int i = 1; i < n + 1; i++) {
      for (long long int j = 1; j < n + 1; j++) {
        a[i][j] = min(a[i][j], a[i][b[k]] + a[b[k]][j]);
      }
    }
    occ[b[k]] = 1;
    long long int ans = 0;
    for (long long int i = 1; i < n + 1; i++) {
      for (long long int j = 1; j < n + 1; j++) {
        if (occ[j] == 1 && occ[i] == 1) ans += a[i][j];
      }
    }
    m[b[k]] = ans;
  }
  for (long long int i = 0; i < n; i++) cout << m[b[i]] << " ";
}
void getZarr(string str, long long int Z[]) {
  Z[0] = 0;
  long long int n = str.length();
  long long int L, R, k;
  L = R = 0;
  for (long long int i = 1; i < n; i++) {
    if (i > R) {
      L = R = i;
      while (R < n && str[R - L] == str[R]) R++;
      Z[i] = R - L;
      R--;
    } else {
      k = i - L;
      if (Z[k] < R - i + 1)
        Z[i] = Z[k];
      else {
        L = i;
        while (R < n && str[R - L] == str[R]) R++;
        Z[i] = R - L;
        R--;
      }
    }
  }
}
long long int modex(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
