#include <bits/stdc++.h>
using namespace std;
const int DBG = 0, INF = int(1e9);
int main() {
  ios_base::sync_with_stdio(0);
  cout.setf(ios::fixed);
  int n;
  cin >> n;
  int adj[n][n];
  for (int _n = (n), i = 0; i < _n; ++i)
    for (int _n = (n), j = 0; j < _n; ++j) cin >> adj[i][j];
  vector<int> x(n);
  for (int _n = (n), i = 0; i < _n; ++i) cin >> x[i];
  vector<long long> res(n);
  int d[n][n];
  for (int _n = (n), i = 0; i < _n; ++i)
    for (int _n = (n), j = 0; j < _n; ++j) d[i][j] = adj[x[i] - 1][x[j] - 1];
  for (int _b = (0), k = (n - 1); k >= _b; --k) {
    long long rs = 0;
    for (int _n = (n), i = 0; i < _n; ++i)
      for (int _n = (n), j = 0; j < _n; ++j) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        if (i >= k && j >= k && i != j) rs += d[i][j];
      }
    res[k] = rs;
  }
  for (__typeof(res.begin()) it = res.begin(); it != res.end(); ++it)
    cout << *it << " ";
  cout << endl;
  return 0;
}
