#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  vector<vector<int>> dist(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> dist[i][j];
    }
  }
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }
  reverse(x.begin(), x.end());
  vector<long long> sol;
  vector<bool> vis(n, false);
  for (int k = 0; k < n; k++) {
    int a = x[k];
    long long ans = 0;
    vis[a] = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(dist[i][a] + dist[a][j], dist[i][j]);
        if (vis[i] && vis[j]) ans += dist[i][j];
      }
    }
    sol.push_back(ans);
  }
  for (int i = 0; i < n; i++) {
    cout << sol[n - 1 - i] << " ";
  }
}
