#include <bits/stdc++.h>
using namespace std;
const long long inf = LLONG_MAX / 2;
const long long maxP = 100000;
bool is_prime[maxP];
void sieve() {
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = false;
  is_prime[1] = false;
  for (long long i = 2; i * i <= maxP; i++) {
    if (is_prime[i]) {
      for (long long j = i * i; j <= maxP; j += i) is_prime[j] = false;
    }
  }
}
long long power(long long b, long long e, long long m) {
  if (e == 0) return 1;
  if (e & 1) return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
long long power(long long b, long long e) {
  if (e == 0) return 1;
  if (e & 1) return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.second < b.second);
}
bool sortbysecdesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return (a.second > b.second);
}
void solve() {
  long long n;
  cin >> n;
  long long visited[n];
  for (long long i = 0; i < n; i++) visited[i] = false;
  long long a[n][n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  vector<long long> d(n);
  for (long long i = 0; i < n; i++) {
    cin >> d[i];
    d[i]--;
  }
  reverse(d.begin(), d.end());
  vector<long long> ans;
  for (long long k = 0; k < n; k++) {
    long long vertex = d[k];
    visited[vertex] = true;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        a[i][j] = min(a[i][j], a[i][vertex] + a[vertex][j]);
      }
    }
    long long sum = 0;
    for (long long i = 0; i < n; i++) {
      if (visited[i] == true) {
        for (long long j = 0; j < n; j++) {
          if (visited[j] == true) sum += a[i][j];
        }
      }
    }
    ans.push_back(sum);
  }
  reverse(ans.begin(), ans.end());
  for (auto it : ans) {
    cout << it << " ";
  }
  cout << "\n";
}
int main() {
  long long t;
  t = 1;
  while (t--) solve();
}
