#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1.0);
template <class T>
inline T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) {
  while (!Q.empty()) Q.pop();
}
inline int random(int l, int r) { return rand() % (r - l + 1) + l; }
int dir_4[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int dir_8[8][2] = {{0, 1},  {-1, 1}, {-1, 0}, {-1, -1},
                   {0, -1}, {1, -1}, {1, 0},  {1, 1}};
int main() {
  int n;
  cin >> n;
  vector<vector<int> > g(n, vector<int>(n));
  vector<bool> mark(n, false);
  vector<int> order(n);
  vector<long long> ans(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> g[i][j];
  for (int i = 0; i < n; i++) cin >> order[i], order[i]--;
  reverse(order.begin(), order.end());
  for (int k = 0; k < n; k++) {
    long long sum = 0;
    mark[order[k]] = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        g[i][j] = min(g[i][order[k]] + g[order[k]][j], g[i][j]);
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (mark[i] && mark[j]) sum += g[i][j];
    ans[n - k - 1] = sum;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " " << ans[i];
    else
      cout << ans[i];
  }
  cout << endl;
}
