#include <bits/stdc++.h>
using namespace std;
const long long maxn = 500 + 10;
const long long INF = 0x3FFFFFFFFFFFFFFFLL;
long long n, b[maxn], d[maxn][maxn], ans[maxn];
int main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) scanf("%I64d", &d[i][j]);
  for (long long i = n; i >= 1; i--) scanf("%I64d", &b[i]);
  for (long long k = 1; k <= n; k++) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (d[i][b[k]] + d[b[k]][j] < d[i][j])
          d[i][j] = d[i][b[k]] + d[b[k]][j];
      }
    }
    for (long long i = 1; i <= k; i++)
      for (long long j = 1; j <= k; j++) ans[k] += d[b[i]][b[j]];
  }
  for (long long i = n; i >= 1; i--) {
    if (i == 1)
      printf("%I64d\n", ans[i]);
    else
      printf("%I64d ", ans[i]);
  }
  return 0;
}
