#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 500 + 10;
long long n, adj[MAX_N][MAX_N], dp[MAX_N][MAX_N], m[MAX_N];
bool check[MAX_N];
vector<long long> ans;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) scanf("%I64d", &adj[i][j]);
  for (long long i = 0; i < n; i++) scanf("%I64d", &m[i]);
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) dp[i][j] = adj[i][j];
  for (long long k = n - 1; k >= 0; k--) {
    long long x = m[k], sum = 0;
    x--;
    check[x] = true;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        if (check[i] && check[j]) {
          dp[x][i] = min(dp[x][i], dp[j][i] + adj[x][j]);
          dp[i][x] = min(dp[i][x], dp[i][j] + adj[j][x]);
        }
      }
    }
    for (long long a = 0; a < n; a++) {
      for (long long b = 0; b < n; b++) {
        if (check[a] && check[b]) {
          dp[a][b] = min(dp[a][b], dp[a][x] + dp[x][b]);
          sum += dp[a][b];
        }
      }
    }
    ans.push_back(sum);
  }
  for (long long i = (long long)ans.size() - 1; i >= 0; i--)
    printf("%I64d ", ans[i]);
  cout << endl;
  return 0;
}
