#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int cas = 1;
int dxx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dyy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
long long ai[502][502], q[502];
long long dp[502][502];
int main() {
  long long n, m, i, j, k, l, sum[502], a, u, v;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) scanf("%lld", &dp[i][j]);
  for (i = 1; i <= n; i++) scanf("%lld", &q[i]);
  memset(sum, 0, sizeof(sum));
  vector<long long> vc;
  for (i = n; i > 0; i--) {
    l = vc.size();
    a = q[i];
    for (j = 0; j < l; j++) {
      u = vc[j];
      for (k = 0; k < l; k++) {
        v = vc[k];
        dp[a][v] = min(dp[a][v], dp[a][u] + dp[u][v]);
      }
    }
    for (j = 0; j < l; j++) {
      u = vc[j];
      for (k = 0; k < l; k++) {
        v = vc[k];
        dp[u][a] = min(dp[u][a], dp[u][v] + dp[v][a]);
      }
    }
    for (j = 0; j < l; j++) {
      u = vc[j];
      for (k = 0; k < l; k++) {
        v = vc[k];
        dp[u][v] = min(dp[u][v], dp[u][a] + dp[a][v]);
      }
    }
    vc.push_back(q[i]);
    l = vc.size();
    for (j = 0; j < l; j++) {
      u = vc[j];
      for (k = 0; k < l; k++) {
        v = vc[k];
        sum[i] += dp[u][v];
      }
    }
  }
  for (i = 1; i <= n; i++) printf("%lld ", sum[i]);
}
