#include <bits/stdc++.h>
using namespace std;
const long long N = 550;
long long n;
long long a[N], ans[N];
long long dis[N][N], e[N][N];
bool vis[N];
inline long long read() {
  long long re = 0, k = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') k = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    re = re * 10 + ch - '0';
    ch = getchar();
  }
  return re * k;
}
inline void get_ans(long long x) {
  long long sum = 0;
  for (register long long i = 1; i <= n; i++) {
    for (register long long j = 1; j <= n; j++) {
      if (vis[i] && vis[j]) sum += dis[i][j];
    }
  }
  ans[x] = sum;
  return;
}
signed main() {
  n = read();
  for (register long long i = 1; i <= n; i++) {
    for (register long long j = 1; j <= n; j++) {
      dis[i][j] = read();
    }
  }
  for (register long long i = 1; i <= n; i++) a[i] = read();
  for (register long long x = n; x; x--) {
    long long k = a[x];
    vis[k] = 1;
    for (register long long i = 1; i <= n; i++) {
      for (register long long j = 1; j <= n; j++) {
        if (i != j) {
          if (dis[i][k] + dis[k][j] < dis[i][j])
            dis[i][j] = dis[i][k] + dis[k][j];
        }
      }
    }
    get_ans(x);
  }
  for (register long long i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
