#include <bits/stdc++.h>
using namespace std;
const int mod = 111539786, N = 1e5 + 1;
long long T, n, x[555], dist[555][555];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> dist[i][j];
    }
  }
  for (long long i = 1; i <= n; i++) {
    cin >> x[i];
  }
  long long v[555];
  for (long long k = n; k >= 1; k--) {
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][x[k]] + dist[x[k]][j]);
      }
    }
    for (long long l = k; l <= n; l++) {
      for (long long r = k; r <= n; r++) {
        ans += dist[x[l]][x[r]];
      }
    }
    v[k] = ans;
  }
  for (long long i = 1; i <= n; i++) {
    cout << v[i] << ' ';
  }
}
