#include <bits/stdc++.h>
using namespace std;
const int N = 506;
long long g[N][N], ans[N], dis[N][N];
int n, id[N];
map<int, int> ihash;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%I64d", &dis[i][j]);
  ihash.clear();
  for (int i = 1, a; i <= n; i++) {
    scanf("%d", &a);
    ihash.insert(make_pair(a, n + 1 - i));
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) g[ihash[i]][ihash[j]] = dis[i][j];
  for (int k = 1; k <= n; k++) {
    ans[k] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    for (int i = 1; i <= k; i++)
      for (int j = 1; j <= k; j++) ans[k] += g[i][j];
  }
  for (int i = n; i >= 1; i--) {
    printf("%I64d", ans[i]);
    if (i == 1)
      printf("\n");
    else
      printf(" ");
  }
  return 0;
}
