#include <bits/stdc++.h>
using namespace std;
long long dis[505][505], ans, w[505][505];
int n;
void floyd(int v) {
  dis[v][v] = 0;
  for (int i = 0; i < n; i++) {
    if (dis[i][i] == 0x3f3f3f3f3f3f3f3f) continue;
    for (int j = 0; j < n; j++) {
      if (dis[j][j] == 0x3f3f3f3f3f3f3f3f) continue;
      if (dis[i][v] > dis[i][j] + w[j][v]) {
        ans += (dis[i][j] + w[j][v]);
        if (dis[i][v] != 0x3f3f3f3f3f3f3f3f) ans -= dis[i][v];
        dis[i][v] = dis[i][j] + w[j][v];
      }
      if (dis[v][i] > dis[j][i] + w[v][j]) {
        ans += (dis[j][i] + w[v][j]);
        if (dis[v][i] != 0x3f3f3f3f3f3f3f3f) ans -= dis[v][i];
        dis[v][i] = dis[j][i] + w[v][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (dis[i][i] == 0x3f3f3f3f3f3f3f3f) continue;
    for (int j = 0; j < n; j++) {
      if (dis[j][j] == 0x3f3f3f3f3f3f3f3f) continue;
      if (dis[i][j] > dis[i][v] + dis[v][j]) {
        ans += (dis[i][v] + dis[v][j]);
        if (dis[i][j] != 0x3f3f3f3f3f3f3f3f) ans -= dis[i][j];
        dis[i][j] = dis[i][v] + dis[v][j];
      }
    }
  }
}
long long Ans[100005];
void dfs(int r) {
  if (r == n) return;
  int v;
  scanf("%d", &v);
  v--;
  dfs(r + 1);
  floyd(v);
  Ans[r] = ans;
}
int main() {
  int V;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        scanf("%d", &V);
        w[i][j] = V;
      }
    }
    memset(dis, 63, sizeof(dis));
    ans = 0;
    dfs(0);
    for (int i = 0; i < n; i++) printf("%I64d ", Ans[i]);
    cout << endl;
  }
}
