#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
int _bufint;
inline int in() {
  scanf("%d", &_bufint);
  return _bufint;
}
const long long LINF = ~((1ll) << 63);
const int INF = ~(1 << 31);
const double eps = 1e-6;
int main() {
  int n = in();
  vector<vector<long long> > g(n, vector<long long>(n, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      g[i][j] = in();
    }
  }
  vector<long long> ans;
  vector<int> v;
  for (int i = 0; i < n; ++i) v.push_back(in() - 1);
  reverse(v.begin(), v.end());
  vector<vector<long long> > dist(n, vector<long long>(n, LINF / 10));
  for (int i = 0; i < n; ++i) dist[i][i] = 0;
  long long res = 0;
  vector<int> on(n, 0);
  for (int i = 0; i < v.size(); ++i) {
    int cur = v[i];
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (!on[j] || !on[k]) continue;
        dist[cur][j] = min(dist[cur][j], dist[k][j] + g[cur][k]);
      }
    }
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (!on[j] || !on[k]) continue;
        dist[j][cur] = min(dist[j][cur], dist[j][k] + g[k][cur]);
      }
    }
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (!on[j] || !on[k]) continue;
        if (dist[j][k] > dist[j][cur] + dist[cur][k]) {
          res -= dist[j][k] - (dist[j][cur] + dist[cur][k]);
          dist[j][k] = dist[j][cur] + dist[cur][k];
        }
      }
    }
    for (int j = 0; j < n; ++j) {
      if (on[j]) {
        res += dist[cur][j];
        res += dist[j][cur];
      }
    }
    on[cur] = 1;
    ans.push_back(res);
  }
  for (int i = n - 1; i >= 0; --i) printf("%I64d ", ans[i]);
  cout << endl;
  return 0;
}
