#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long Array[n + 1][n + 1];
  long long dp[n + 1][n + 1];
  long long sum = 0;
  memset(Array, 0, sizeof(Array));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> Array[i][j];
      dp[i][j] = Array[i][j];
    }
  }
  int delet[n + 1];
  bool visit[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> delet[i];
    visit[i] = false;
  }
  vector<long long> v;
  visit[delet[n]] = true;
  for (int k = n; k > 0; k--) {
    long long sum = 0;
    visit[delet[k]] = true;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][delet[k]] + dp[delet[k]][j]);
        if (visit[i] == true && visit[j] == true) sum += dp[i][j];
      }
    }
    v.push_back(sum);
  }
  for (int i = n - 1; i >= 0; i--) {
    cout << v[i] << " ";
  }
}
