#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
signed main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long dp[n][n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      cin >> dp[i][j];
    }
  }
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<long long> ans(n, 0);
  vector<bool> visited(n, false);
  for (long long i = n - 1; i >= 0; i--) {
    visited[arr[i] - 1] = true;
    for (long long j = 0; j < n; j++) {
      for (long long k = 0; k < n; k++) {
        dp[j][k] = min(dp[j][k], dp[j][arr[i] - 1] + dp[arr[i] - 1][k]);
        if (visited[j] and visited[k]) {
          ans[i] += dp[j][k];
        }
      }
    }
  }
  for (auto i : ans) cout << i << ' ';
  cout << endl;
  return 0;
}
