#include <bits/stdc++.h>
using namespace std;
const long long N = 505;
long long g[N][N];
long long adj[N][N];
long long ans[N];
long long mn[N][N];
long long n, w;
const long long mx = INT_MAX;
long long order[N];
int32_t main() {
  cin >> n;
  ans[n] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> w;
      g[i][j] = w;
    }
  }
  for (long long i = 0; i < n; i++) cin >> order[i + 1];
  for (long long i = n; i >= 1; i--) {
    long long x = order[i];
    for (long long j = n; j >= 1; j--) {
      long long u = order[j];
      for (long long k = n; k >= 1; k--) {
        long long v = order[k];
        g[u][v] = min(g[u][v], g[u][x] + g[x][v]);
      }
    }
    for (long long j = i; j <= n; j++) {
      long long u = order[j];
      for (long long k = i; k <= n; k++) {
        long long v = order[k];
        ans[i] += g[u][v];
      }
    }
  }
  for (long long i = 1; i <= n; i++) cout << ans[i] << ' ';
  cout << endl;
  return 0;
}
