#include <bits/stdc++.h>
using namespace std;
const long long INF = 9e18;
const long long MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long n;
  cin >> n;
  vector<vector<long long> > adj(n, vector<long long>(n));
  for (auto &i : adj) {
    for (auto &j : i) {
      cin >> j;
    }
  }
  vector<long long> x(n);
  auto g = adj;
  for (long long i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }
  reverse(x.begin(), x.end());
  vector<long long> ans;
  vector<long long> inplace;
  for (auto &i : adj) {
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      for (long long k = 0; k < n; k++) {
        adj[j][k] = min(adj[j][k], adj[j][x[i]] + adj[x[i]][k]);
      }
    }
    inplace.push_back(x[i]);
    ans.emplace_back(0);
    for (auto &j : inplace) {
      for (auto &k : inplace) {
        ans.back() += adj[j][k];
      }
    }
    for (auto &i : adj) {
    }
  }
  for (long long i = n - 1; i >= 0; i--) {
    cout << ans[i] << " \n"[i == 0];
  }
}
