#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
inline void cn(long& n) {
  n = 0;
  long ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
  n = n * sign;
}
template <class T>
void cmin(T& a, T b) {
  if (b < a) a = b;
}
template <class T>
void cmax(T& a, T b) {
  if (b > a) a = b;
}
template <class T>
int len(const T& c) {
  return (int)c.size();
}
template <class T>
int len(char c[]) {
  return (int)strlen(c);
}
string itos(long n) {
  string s;
  while (n) {
    s += (n % 10 + 48);
    n /= 10;
  }
  reverse(s.begin(), s.end());
  return s;
}
long stoi(string s) {
  long n = 0;
  for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48);
  return n;
}
int main() {
  long n, ar[505][505], rem[505], col[505] = {0}, p;
  long long res[505], tot;
  cin >> n;
  for (int i(0), _n(n); i < _n; ++i)
    for (int j(0), _n(n); j < _n; ++j) cin >> ar[i][j];
  for (int i(0), _n(n); i < _n; ++i) {
    cin >> rem[i];
    rem[i]--;
  }
  reverse(rem, rem + n);
  for (int i(0), _n(n); i < _n; ++i) {
    p = rem[i];
    col[p] = 1;
    for (int j(0), _n(n); j < _n; ++j) {
      for (int k(0), _n(n); k < _n; ++k) {
        cmin(ar[j][k], ar[j][p] + ar[p][k]);
      }
    }
    tot = 0;
    for (int j(0), _n(n); j < _n; ++j)
      for (int k(0), _n(n); k < _n; ++k) {
        if (col[j] && col[k]) tot += ar[j][k];
      }
    res[i] = tot;
  }
  for (int i = n - 1; i >= 0; i--) cout << res[i] << " ";
  return 0;
}
