#include <bits/stdc++.h>
constexpr int MAX_N = 505;
using namespace std;
int n;
int adj[MAX_N][MAX_N];
int x[MAX_N];
long long ans[MAX_N];
bool used[MAX_N];
int main() {
  scanf(" %d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf(" %d", &adj[i][j]);
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    scanf(" %d", &x[i]);
    x[i]--;
  }
  for (int idx = 0; idx < n; idx++) {
    int k = x[idx];
    used[k] = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
    long long cur = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (!used[i] || !used[j]) continue;
        cur += adj[i][j];
      }
    }
    ans[idx] = cur;
  }
  for (int i = n - 1; i >= 0; i--) {
    printf("%lld ", ans[i]);
  }
  printf("\n");
  return 0;
}
