#include <bits/stdc++.h>
using namespace std;
int vis[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int dis[n][n];
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> dis[i][j];
  for (int i = 0; i < n; i++) cin >> arr[i];
  reverse(arr.begin(), arr.end());
  vector<long long> ans;
  for (int k = 0; k < n; k++) {
    int x = arr[k] - 1;
    vis[x] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dis[i][j] = min(dis[i][j], dis[i][x] + dis[x][j]);
      }
    }
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      if (!vis[i]) continue;
      for (int j = 0; j < n; j++)
        if (vis[j]) sum += dis[i][j];
    }
    ans.push_back(sum);
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
  return 0;
}
