#include <bits/stdc++.h>
using namespace std;
long long fact[1000000];
long long factorial(long long n) {
  fact[0] = 1;
  for (long long i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i);
  }
  return fact[n];
}
long long power(long long x, long long y) {
  long long temp;
  if (y == 0) return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}
long long nCk(long long n, long long k) {
  if (k > n - k) k = n - k;
  long long ans = 1;
  for (long long i = 0; i < k; i++) {
    ans *= (n - i);
    ans /= (i + 1);
  }
  return ans;
}
vector<long long> factorization(long long n) {
  vector<long long> factors;
  for (long long d = 2; d * d <= n; d++) {
    while (n % d == 0) {
      factors.push_back(d);
      n /= d;
    }
  }
  if (n > 1) factors.push_back(n);
  return factors;
}
vector<long long> divisors(long long n) {
  vector<long long> divisors;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        divisors.push_back(i);
      else
        divisors.push_back(i), divisors.push_back(n / i);
    }
  }
  return divisors;
}
long long sieve[1000001];
void SOE() {
  sieve[0] = 0;
  sieve[1] = 0;
  for (long long i = 2; i <= 1000000; i++) {
    if (sieve[i]) {
      for (long long j = 2; j * i < 1000000; j++) sieve[j * i] = 0;
    }
  }
}
bool isPrime(long long x) {
  if (x == 1) return 0;
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i == 0) return false;
  }
  return true;
}
const long long INF = 1e5 + 5;
long long vis[600], res[600], x[600];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  while (t--) {
    long long n;
    cin >> n;
    vector<vector<long long>> mat(n, vector<long long>(n));
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) cin >> mat[i][j];
    }
    for (long long i = 0; i < n; i++) cin >> x[i], x[i]--;
    for (long long k = n - 1; k >= 0; k--) {
      vis[x[k]] = 1;
      for (long long i = 0; i < n; i++) {
        for (long long j = 0; j < n; j++) {
          if (mat[i][x[k]] + mat[x[k]][j] < mat[i][j])
            mat[i][j] = mat[i][x[k]] + mat[x[k]][j];
        }
      }
      for (long long i = 0; i < n; i++) {
        for (long long j = 0; j < n; j++) {
          if (vis[i] and vis[j]) {
            res[k] += mat[i][j];
          }
        }
      }
    }
    for (long long i = 0; i < n; i++) cout << res[i] << " ";
  }
}
