#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.141592653589793238462643383;
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int Ceil(long long int a, long long int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
const int MAX = 200009;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 10;
long long int a[509][509], b[509][509];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  (memset((a), 0, sizeof(a))), (memset((b), 0, sizeof(b)));
  int n;
  cin >> n;
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j) cin >> a[i][j];
  vector<int> tmp;
  for (int i = 0; i < (n); ++i) {
    int t;
    cin >> t;
    tmp.push_back(t);
  }
  for (int i = (n - 1); i >= (0); --i) b[0][n - i] = tmp[i];
  vector<long long int> res;
  for (int i = (1); i <= (n); ++i) {
    int c = b[0][i];
    b[c][c] = 0;
    for (int j = (1); j <= (i - 1); ++j) {
      long long int m1 = 1e18, m2 = 1e18;
      int u = b[0][j];
      for (int k = (1); k <= (i - 1); ++k) {
        int v = b[0][k];
        m1 = min(m1, a[c][v] + b[k][j]);
        m2 = min(m2, a[v][c] + b[j][k]);
      }
      b[i][j] = m1;
      b[j][i] = m2;
    }
    for (int j = (1); j <= (i - 1); ++j)
      for (int k = (1); k <= (i - 1); ++k) {
        b[j][k] = min(b[j][k], b[j][i] + b[i][k]);
        b[k][j] = min(b[k][j], b[k][i] + b[i][j]);
      }
    long long int ans = 0;
    for (int j = (1); j <= (i); ++j)
      for (int k = (1); k <= (i); ++k) ans += b[j][k];
    res.push_back(ans);
  }
  reverse((res).begin(), (res).end());
  for (auto&(it) : (res)) cout << it << ' ';
  return 0;
}
