#include <bits/stdc++.h>
using namespace std;
long long n;
vector<vector<long long> > d(600, vector<long long>(600));
vector<long long> a(610), ans(610);
vector<long long> present(610);
void FloydWarshall() {
  for (int k = 1; k <= n; k++) {
    present[a[k]] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        d[i][j] = min(d[i][j], d[i][a[k]] + d[a[k]][j]);
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        if (present[i] && present[j]) ans[k] += d[i][j];
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> d[i][j];
  for (int i = n; i >= 1; i--) cin >> a[i];
  FloydWarshall();
  for (int i = n; i >= 1; i--) cout << ans[i] << " ";
  return 0;
}
