#include <bits/stdc++.h>
using namespace std;
const long long OO = 1e8;
const long long OOL = 1e18;
const long long MOD = 1e9 + 7;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  return (b == 0 ? a : gcd(b, a % b));
}
unsigned long long lcm(unsigned long long a, unsigned long long b) {
  return (a * (b / gcd(a, b)));
}
unsigned long long power(unsigned long long a, unsigned long long b) {
  if (b == 0) return 1;
  unsigned long long ans = power(a, b / 2);
  ans = ans * ans;
  if (b % 2 != 0) ans *= a;
  return ans;
}
int n;
int G[501][501];
int alias[501];
long long ans[555];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < (int)(n); ++i)
    for (int j = 0; j < (int)(n); ++j) cin >> G[i][j];
  for (int i = 0; i < (int)(n); ++i)
    cin >> alias[n - i - 1], alias[n - i - 1]--;
  for (int k = 0; k < (int)(n); ++k) {
    for (int i = 0; i < (int)(n); ++i) {
      for (int j = 0; j < (int)(n); ++j) {
        int ii = alias[i];
        int jj = alias[j];
        int kk = alias[k];
        G[ii][jj] = min(G[ii][jj], G[ii][kk] + G[kk][jj]);
        if (i <= k && j <= k) ans[k] += G[ii][jj];
      }
    }
  }
  reverse(ans, ans + n);
  for (int i = 0; i < (int)(n); ++i) cout << ans[i] << " ";
  return 0;
}
