#include <bits/stdc++.h>
using namespace std;
long long min(long long a, long long b, long long c) {
  return min(a, min(b, c));
}
int main() {
  long long n;
  cin >> n;
  long long w[n + 1][n + 1];
  vector<long long> done(n + 1, false);
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> w[i][j];
  long long x[n + 1];
  for (long long i = 1; i <= n; i++) cin >> x[n - i + 1];
  long long dp[n + 1][n + 1];
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) {
      if (i == j)
        dp[i][j] = 0;
      else
        dp[i][j] = 1000000007;
    }
  vector<long long> ans;
  for (long long k = 1; k <= n; k++) {
    vector<long long> temp;
    for (long long i = 1; i <= n; i++)
      if (done[i]) temp.push_back(i);
    for (long long i = 0; i <= int(temp.size()) - 1; i++)
      for (long long j = 0; j <= int(temp.size()) - 1; j++) {
        long long i1 = temp[i];
        long long i2 = temp[j];
        dp[x[k]][i1] =
            min(dp[x[k]][i1], w[x[k]][i1], (w[x[k]][i2] + dp[i2][i1]));
        dp[i1][x[k]] =
            min(dp[i1][x[k]], w[i1][x[k]], (dp[i1][i2] + w[i2][x[k]]));
      }
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++)
        dp[i][j] = min(dp[i][j], dp[i][x[k]] + dp[x[k]][j]);
    done[x[k]] = true;
    long long sum = 0;
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++)
        if (dp[i][j] != 1000000007) sum += dp[i][j];
    ans.push_back(sum);
  }
  reverse(ans.begin(), ans.end());
  for (long long x : ans) cout << x << " ";
  return 0;
}
