#include <bits/stdc++.h>
using namespace std;
const int MAXN = 512;
const int INF = 0x3f3f3f3f;
long long dis[MAXN][MAXN], a[MAXN], ans[MAXN];
bool in[MAXN];
int n;
void Floyd() {
  for (int k = n; k >= 1; k--) {
    int t = a[k];
    in[t] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dis[i][j] = min(dis[i][j], dis[i][t] + dis[t][j]);
        if (in[i] && in[j]) ans[k] += dis[i][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
  puts("");
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%lld", &dis[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  Floyd();
  return 0;
}
