#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
template <class T>
T power(T N, T P) {
  return (P == 0) ? 1 : N * power(N, P - 1);
}
template <class T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
void make_unique(vector<int>& a) {
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
}
const int N = 500 + 5;
int d[N][N];
long long int ans[N];
int ord[N];
bool ok[N];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (__typeof(n) i = 1; i <= n; i++)
      for (__typeof(n) j = 1; j <= n; j++) scanf("%d", &d[i][j]);
    for (__typeof(n) i = 1; i <= n; i++) scanf("%d", &ord[i]);
    memset(ok, false, sizeof(ok));
    for (int kk = n; kk >= 1; kk--) {
      int k = ord[kk];
      ok[k] = true;
      for (__typeof(n) i = 1; i <= n; i++) {
        for (__typeof(n) j = 1; j <= n; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
      long long int tot = 0;
      for (__typeof(n) i = 1; i <= n; i++) {
        for (__typeof(n) j = 1; j <= n; j++) {
          if (ok[i] && ok[j]) {
            tot += d[i][j];
          }
        }
      }
      ans[kk] = tot;
    }
    for (__typeof(n) i = 1; i <= n; i++) printf("%lld ", ans[i]);
    printf("\n");
  }
  return 0;
}
