#include <bits/stdc++.h>
using namespace std;
const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
          d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template <typename T_n>
void read(T_n &n) {
  cin >> n;
}
template <typename T_n, typename T_a>
void read(T_a a[], T_n n) {
  for (int i = 0; i < n; ++i) cin >> a[i];
}
template <typename T_n, typename T_a>
void read(vector<T_a> &a, T_n n) {
  a.resize(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
}
void _init_() { ios_base::sync_with_stdio(0), cin.tie(0); }
const int N = 500 + 9, M = 10 + 9;
const int Mod_M = 1e8;
long long mat[N][N];
int n, sorted[N];
bool vis[N];
stack<long long> ans;
void floyed() {
  for (int idx = 0; idx < n; ++idx) {
    int k = sorted[idx];
    vis[k] = 1;
    long long res = 0;
    for (int a = 0; a < n; ++a)
      for (int b = 0; b < n; ++b) {
        auto newpath = mat[a][k] + mat[k][b];
        if (newpath < mat[a][b]) mat[a][b] = newpath;
        if (vis[a] && vis[b]) res += mat[a][b];
      }
    ans.push(res);
  }
}
void solve() {
  memset(mat, '?', sizeof mat);
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cin >> mat[i][j];
  for (int i = n - 1; ~i; --i) {
    cin >> sorted[i];
    sorted[i]--;
  }
  floyed();
  bool flag = 0;
  while (!ans.empty()) {
    if (flag) cout << " ";
    cout << ans.top();
    flag = 1;
    ans.pop();
  }
  cout << '\n';
}
int main() {
  _init_();
  solve();
  return 0;
}
