#include <bits/stdc++.h>
using namespace std;
inline long long add(long long a, long long b) {
  return ((a % 1000000007LL) + (b % 1000000007LL)) % 1000000007LL;
}
inline long long mul(long long a, long long b) {
  return ((a % 1000000007LL) * (b % 1000000007LL)) % 1000000007LL;
}
inline long long sub(long long a, long long b) {
  return ((a % 1000000007LL) - (b % 1000000007LL) + 1000000007LL) %
         1000000007LL;
}
void Flash() {
  long long n;
  cin >> n;
  long long adj[n][n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      cin >> adj[i][j];
    }
  }
  long long x[n];
  for (long long i = 0; i < n; i++) {
    cin >> x[i];
    --x[i];
  }
  long long minW[n][n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      minW[i][j] = adj[i][j];
    }
  }
  vector<long long> addedVer;
  long long ans[n];
  long long tempAns;
  for (long long i = n - 1; i >= 0; i--) {
    tempAns = 0;
    addedVer.push_back(x[i]);
    for (long long j = 0; j < n; j++) {
      for (long long k = 0; k < n; k++) {
        minW[j][k] = min(minW[j][k], minW[j][x[i]] + minW[x[i]][k]);
      }
    }
    for (auto it : addedVer) {
      for (auto it2 : addedVer) {
        tempAns += minW[it][it2];
      }
    }
    ans[i] = tempAns;
  }
  for (long long i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << "\n";
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  ;
  long long t = 1;
  for (long long i = 1; i <= t; i++) {
    Flash();
  }
  return 0;
}
