#include <bits/stdc++.h>
using namespace std;
const long long int Maxn = 5 * 100 + 5, N = 100 + 5,
                    Mod = 1000 * 1000 * 1000 + 7,
                    inf =
                        (long long int)1000 * 1000 * 1000 * 1000 * 1000 * 1000;
int n, x[Maxn];
long long int dp[Maxn][Maxn], e[Maxn][Maxn];
deque<long long int> ans;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> e[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = inf;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    int xp = x[i];
    for (int j = n - 1; j >= i; j--) {
      for (int k = n - 1; k >= i; k--) {
        dp[xp][x[k]] = min(dp[xp][x[k]], e[xp][x[j]] + dp[x[j]][x[k]]);
        dp[x[k]][xp] = min(dp[x[k]][xp], dp[x[k]][x[j]] + e[x[j]][xp]);
      }
      dp[xp][x[j]] = min(dp[xp][x[j]], e[xp][x[j]]);
      dp[x[j]][xp] = min(dp[x[j]][xp], e[x[j]][xp]);
    }
    for (int j = n - 1; j >= i; j--) {
      for (int k = n - 1; k >= i; k--) {
        dp[x[j]][x[k]] = min(dp[x[j]][x[k]], dp[x[j]][xp] + dp[xp][x[k]]);
      }
    }
    long long int sum = 0;
    for (int j = n - 1; j >= i; j--) {
      for (int k = n - 1; k >= i; k--) {
        sum += dp[x[j]][x[k]];
      }
    }
    ans.push_back(sum);
  }
  while (ans.size()) {
    cout << ans.back() << ' ';
    ans.pop_back();
  }
  return 0;
}
