#include <bits/stdc++.h>
using namespace std;
int INF = 1e9;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> adj(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> adj[i][j];
    }
  }
  vector<bool> vis(n);
  vector<int> order(n);
  for (int i = 0; i < n; ++i) cin >> order[i];
  reverse(order.begin(), order.end());
  vector<long long> ans;
  for (int i = 0; i < n; ++i) {
    int curr = order[i] - 1;
    for (int u = 0; u < n; ++u) {
      for (int v = 0; v < n; ++v) {
        adj[u][v] = min(adj[u][v], adj[u][curr] + adj[curr][v]);
      }
    }
    long long result = 0;
    for (int u = 0; u <= i; ++u) {
      for (int v = 0; v <= i; ++v) {
        int a, b;
        a = order[u] - 1;
        b = order[v] - 1;
        result += adj[a][b];
      }
    }
    ans.push_back(result);
  }
  for (int i = 0; i < n; ++i) {
    cout << ans.back() << " ";
    ans.pop_back();
  }
  cout << "\n";
}
