#include <bits/stdc++.h>
using namespace std;
int pow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res *= x;
    y >>= 1;
    x *= x;
  }
  return res;
}
struct Compare {
  constexpr bool operator()(
      pair<long long, pair<long long, long long> > const& a,
      pair<long long, pair<long long, long long> > const& b) const noexcept {
    return a.first < b.first ||
           (a.first == b.first && a.second.first > b.second.first);
  }
};
void prefix_function(string second, long long arr[]) {
  long long border = 0;
  arr[0] = 0;
  for (long long i = 1; i < second.size(); i++) {
    while (border > 0 && second[i] != second[border]) {
      border = arr[border - 1];
    }
    if (second[i] == second[border]) {
      border++;
    } else {
      border = 0;
    }
    arr[i] = border;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  bool vis[505];
  memset(vis, false, 505);
  long long dist[n + 2][n + 2];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> dist[i][j];
    }
  }
  long long arr[n + 2];
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  vector<long long> sura;
  for (long long k = n; k >= 1; k--) {
    long long res = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (dist[i][arr[k]] + dist[arr[k]][j] < dist[i][j]) {
          dist[i][j] = dist[i][arr[k]] + dist[arr[k]][j];
        }
      }
    }
    vis[arr[k]] = true;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (vis[i] && vis[j]) {
          res += dist[i][j];
        }
      }
    }
    sura.push_back(res);
  }
  for (long long i = n - 1; i >= 0; i--) {
    cout << sura[i] << " ";
  }
  cout << endl;
}
