#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long ans = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 1) + (ans << 3) + ch - '0';
    ch = getchar();
  }
  return ans * f;
}
struct query {
  long long num, p, ans;
} que[509];
long long dis[509][509];
long long a[509][509];
bool cmp(query a, query b) {}
bool vis[509];
int main() {
  long long n = read();
  memset(dis, 0x3f, sizeof(dis));
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= n; ++j) {
      dis[i][j] = read();
    }
  }
  for (long long i = 1; i <= n; ++i) {
    que[i].num = 1;
    que[i].p = read();
  }
  for (long long i = 1; i <= n; ++i) {
    dis[i][i] = 0;
  }
  for (long long l = n; l >= 1; --l) {
    long long k = que[l].p;
    vis[k] = 1;
    for (long long i = 1; i <= n; ++i) {
      for (long long j = 1; j <= n; ++j) {
        if (i != j) {
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
          if (vis[i] && vis[j]) {
            que[l].ans += dis[i][j];
          }
        }
      }
    }
  }
  for (long long i = 1; i <= n; ++i) {
    printf("%lld ", que[i].ans);
  }
}
