#include <bits/stdc++.h>
using namespace std;
class Graph {
  int n;
  vector<vector<long long> > m;
  vector<long long> order;

 public:
  Graph(int _n) {
    n = _n;
    m.assign(n, vector<long long>(n, 0));
  }
  inline void set(int a, int b, int d) { m[a][b] = d; }
  inline void add_order(int i) { order.push_back(i); }
  inline vector<long long> floyd() {
    vector<long long> res;
    reverse(order.begin(), order.end());
    for (int k = 0; k < n; ++k) {
      long long sum = 0;
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) {
          if (m[order[i]][order[k]] + m[order[k]][order[j]] <
              m[order[i]][order[j]]) {
            m[order[i]][order[j]] =
                m[order[i]][order[k]] + m[order[k]][order[j]];
          }
          if (i <= k && j <= k) {
            sum += m[order[i]][order[j]];
          }
        }
      res.push_back(sum);
    }
    return res;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  Graph G(n);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      int x;
      cin >> x;
      G.set(i, j, x);
    }
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    G.add_order(x - 1);
  }
  vector<long long> res = G.floyd();
  for (int i = n - 1; i >= 0; --i) {
    cout << res[i] << " ";
  }
  cout << endl;
}
