#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7, M = 2 * N;
const long long OO = 0x3f3f3f3f3f3f3f3f;
long long adj[N][N];
int n, vis[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int v;
      scanf("%d", &v);
      adj[i][j] = v;
    }
  }
  vector<int> order(n);
  for (int i = 0; i < n; ++i) {
    int &u = order[i];
    scanf("%d", &u);
    --u;
  }
  reverse(order.begin(), order.end());
  vector<long long> ans;
  for (int k : order) {
    vis[k] = 1;
    long long sum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
        if (vis[i] && vis[j]) sum += adj[i][j];
      }
    }
    ans.emplace_back(sum);
  }
  reverse(ans.begin(), ans.end());
  for (long long v : ans) {
    printf("%lld ", v);
  }
  puts("");
}
