#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
vector<long long> v, ans;
long long floyd[505][505] = {};
bool vis[505] = {};
int main() {
  ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),
      cout.tie(0);
  ;
  int n;
  cin >> n;
  for (int i = 1; i <= (int)n; ++i)
    for (int j = 1; j <= (int)n; ++j) cin >> floyd[i][j];
  for (int i = 1; i <= (int)n; ++i) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  reverse(v.begin(), v.end());
  for (auto u : v) {
    long long answer = 0;
    vis[u] = true;
    for (int i = 1; i <= (int)n; ++i)
      for (int j = 1; j <= (int)n; ++j)
        floyd[i][j] = min(floyd[i][j], floyd[i][u] + floyd[u][j]);
    for (int i = 1; i <= (int)n; ++i)
      for (int j = 1; j <= (int)n; ++j)
        if (vis[i] && vis[j]) answer += floyd[i][j];
    ans.push_back(answer);
  }
  reverse(ans.begin(), ans.end());
  for (auto u : ans) cout << u << " ";
  return 0;
}
