#include <bits/stdc++.h>
using namespace std;
long long n, i, j;
vector<long long> v, Ans;
long long adj[504][504], dist[504][504];
bool visit[1000000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      cin >> adj[i][j];
      dist[i][j] = adj[i][j];
    }
  }
  long long x;
  for (i = 1; i <= n; i++) cin >> x, v.push_back(x);
  reverse(v.begin(), v.end());
  int f = 0;
  for (auto k : v) {
    f++;
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++)
        dist[v[i]][v[j]] = min(dist[v[i]][v[j]], dist[v[i]][k] + dist[k][v[j]]);
    }
    for (i = 0; i < f; i++)
      for (j = 0; j < f; j++) ans += dist[v[i]][v[j]];
    Ans.push_back(ans);
  }
  reverse(Ans.begin(), Ans.end());
  for (auto i : Ans) cout << i << " ";
}
