#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const long long int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
const long long int inf = 1e12;
long long int arr[505][505], adj[505][505];
void cp() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> arr[i][j];
  vector<int> removed_nodes(n);
  for (int &x : removed_nodes) cin >> x;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) adj[i][j] = (i == j ? 0 : inf);
  vector<long long int> result;
  vector<int> exist;
  while (!removed_nodes.empty()) {
    int x = removed_nodes.back();
    x--;
    removed_nodes.pop_back();
    exist.push_back(x);
    for (int y : exist) adj[y][x] = arr[y][x], adj[x][y] = arr[x][y];
    for (int i : exist)
      for (int j : exist)
        if (adj[i][j] < inf && adj[j][x] < inf)
          adj[i][x] = min(adj[i][x], adj[i][j] + adj[j][x]);
    for (int i : exist)
      for (int j : exist)
        if (adj[x][j] < inf && adj[j][i] < inf)
          adj[x][i] = min(adj[x][i], adj[x][j] + adj[j][i]);
    for (int i : exist)
      for (int j : exist)
        if (adj[i][x] < inf && adj[x][j] < inf)
          adj[i][j] = min(adj[i][j], adj[i][x] + adj[x][j]);
    long long int ans = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (adj[i][j] < inf) ans += adj[i][j];
    result.push_back(ans);
  }
  while (!result.empty()) cout << result.back() << " ", result.pop_back();
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  t = 1;
  while (t--) {
    cp();
  }
  return 0;
}
