#include <bits/stdc++.h>
using namespace std;
long long dis[1100][1100];
int vet[1100];
bool vis[1100];
long long ans[1100];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%I64d", &dis[i][j]);
  for (int i = 1; i <= n; i++) scanf("%d", &vet[i]);
  memset(vis, false, sizeof(vis));
  for (int i = n; i >= 1; i--) {
    int u = vet[i];
    vis[u] = true;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        if (dis[j][u] + dis[u][k] < dis[j][k])
          dis[j][k] = dis[j][u] + dis[u][k];
    for (int j = 1; j <= n; j++)
      if (vis[j])
        for (int k = 1; k <= n; k++)
          if (vis[k]) ans[i] += dis[j][k];
  }
  printf("%I64d", ans[1]);
  for (int i = 2; i <= n; i++) printf(" %I64d", ans[i]);
  puts("");
}
