#include <bits/stdc++.h>
using namespace std;
int n, a[505][505], x[505];
long long d[505][505];
vector<long long> ans;
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j) {
      scanf("%d", &a[i][j]);
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = (long long)a[i][j];
    }
  for (int i = (1); i <= (n); ++i) scanf("%d", &x[i]);
  for (int k = (n); k >= (1); --k) {
    for (int u = (1); u <= (n); ++u)
      for (int v = (1); v <= (n); ++v) {
        d[u][v] = min(d[u][v], d[u][x[k]] + d[x[k]][v]);
        d[v][u] = min(d[v][u], d[v][x[k]] + d[x[k]][u]);
      }
    long long s = 0LL;
    for (int i = (n); i >= (k); --i)
      for (int j = (n); j >= (k); --j) s += d[x[i]][x[j]];
    ans.push_back(s);
  }
  for (int i = ((int)ans.size() - 1); i >= (0); --i) cout << ans[i] << " ";
  return 0;
}
