#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 1000000007
#define MAX 500005
#define INF MOD
#define endl "\n"
lli arr[MAX];
lli fw[505][505];
lli md[505][505];
lli order[505];
lli n;
bool active[505];
lli sum(){
    lli s = 0;
    for(lli i = 0; i<n; i++){
        for(lli j = 0; j<n; j++){
            if(active[i] && active[j] &&fw[i][j] != INF){
                //debug(i,j);
                s += fw[i][j];
            }
        }
    }
    return s;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    t = 1;
  //  cin>>t;
    while(t--){
        cin>>n;
        for(lli i = 0; i<n; i++)
            for(lli j = 0; j<n; j++)
                cin>>md[i][j];
        for(lli i = 0; i<n; i++){
            cin>>order[i];
            order[i]--;
        }
        for(lli i = 0; i<n; i++){
            for(lli j = 0; j<n; j++){
                fw[i][j] = md[i][j];
                fw[j][i] = md[j][i];
            }
        }
        vector <lli> ans;
        for(lli i = n-1; i>=0; i--){
            lli medium = order[i];
            active[order[i]] = true;
            for(lli j = 0; j<n; j++){
                for(lli k = 0; k<n; k++){
                    fw[j][k] = min(fw[j][k], fw[j][order[i]] + fw[order[i]][k]);
                }
            }
            //debug(fw[1][2], fw[2][1]);
            ans.push_back(sum());
        }
        reverse(ans.begin(),ans.end());
        for(auto i : ans)
            cout<<i<<" ";
    }
}


#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 1000000007
#define MAX 500005
#define INF MOD
#define endl "\n"
lli arr[MAX];
lli fw[505][505];
lli md[505][505];
lli order[505];
lli n;
bool active[505];
lli sum(){
    lli s = 0;
    for(lli i = 0; i<n; i++){
        for(lli j = 0; j<n; j++){
            if(active[i] && active[j] &&fw[i][j] != INF){
                //debug(i,j);
                s += fw[i][j];
            }
        }
    }
    return s;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    t = 1;
  //  cin>>t;
    while(t--){
        cin>>n;
        for(lli i = 0; i<n; i++)
            for(lli j = 0; j<n; j++)
                cin>>md[i][j];
        for(lli i = 0; i<n; i++){
            cin>>order[i];
            order[i]--;
        }
        for(lli i = 0; i<n; i++){
            for(lli j = 0; j<n; j++){
                if(i != j){
                    fw[i][j] = md[i][j];
                    fw[j][i] = md[j][i];
                }
            }
        }
        active[order[n-1]] = true;
        vector <lli> ans(1,0);
        for(lli i = n-2; i>=0; i--){
            lli medium = order[i];
            active[order[i]] = true;
            for(lli j = 0; j<n; j++){
                for(lli k = 0; k<n; k++){
                    fw[j][k] = min(fw[j][k], fw[j][order[i]] + fw[order[i]][k]);
                }
            }
            //debug(fw[1][2], fw[2][1]);
            ans.push_back(sum());
        }
        reverse(ans.begin(),ans.end());
        for(auto i : ans)
            cout<<i<<" ";
    }
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
ll arr[501][501];
vector<int>D;
vector<int>v1(501);
vector<int>ans;
ll t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
void floyd()
{
    ll dist[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            dist[i][j]=arr[i][j];
        }
    }
    for(int k=n-1;k>=0;k--)
    {
        //cout<<"x[k] "<<v1[k]<<endl;
        mp[v1[k]]=1;
        int suma=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
              	if(mp[i]==1 and mp[j]==1)
                dist[i][j]=min(dist[i][j],dist[i][v1[k]]+dist[v1[k]][j]);

            }

        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(mp[i]==1 and mp[j]==1)
                {
                    suma+=dist[i][j];
                }
            }
        }
       // cout<<suma<<endl;
        ans.pb(suma);
    }
    //reverse(ans.begin(),ans.end());
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    while(t--)
    {
        cin>>n;
        //cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }

        for(int i=0;i<n;i++)
        cin>>v1[i],v1[i]-=1;

        //cout<<endl;
        floyd();
        reverse(ans.begin(),ans.end());
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
ll arr[501][501];
vector<int>D;
vector<int>v1(501);
vector<int>ans;
ll t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
void floyd()
{
    ll dist[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            dist[i][j]=arr[i][j];
        }
    }
    vector<int>check(n+1,0);
    for(int k=n-1;k>=0;k--)
    {
        //cout<<"x[k] "<<v1[k]<<endl;
        check[v1[k]]=1;
        int suma=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                dist[i][j]=min(dist[i][j],dist[i][v1[k]]+dist[v1[k]][j]);

            }

        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(check[i] and check[j])
                {
                    suma+=dist[i][j];
                }
            }
        }
       // cout<<suma<<endl;
        ans.pb(suma);
    }
    //reverse(ans.begin(),ans.end());
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    while(t--)
    {
        cin>>n;
        //cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }

        for(int i=0;i<n;i++)
        cin>>v1[i],v1[i]-=1;

        //cout<<endl;
        floyd();
        reverse(ans.begin(),ans.end());
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
int arr[501][501];
vector<int>D;
vector<int>v1(501);
vector<int>ans;
int t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
void floyd()
{
    int dist[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            dist[i][j]=arr[i][j];
        }
    }
    vector<int>check(n+1,0);
    for(int k=n-1;k>=0;k--)
    {
        //cout<<"x[k] "<<v1[k]<<endl;
        check[v1[k]]=1;
        int suma=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                dist[i][j]=min(dist[i][j],dist[i][v1[k]]+dist[v1[k]][j]);

            }

        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(check[i] and check[j])
                {
                    suma+=dist[i][j];
                }
            }
        }
       // cout<<suma<<endl;
        ans.pb(suma);
    }
    //reverse(ans.begin(),ans.end());
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    while(t--)
    {
        cin>>n;
        //cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }

        for(int i=0;i<n;i++)
        cin>>v1[i],v1[i]-=1;

        //cout<<endl;
        floyd();
        reverse(ans.begin(),ans.end());
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
ll arr[501][501];
vector<int>D;
vector<int>v1(501);
vector<int>ans;
ll t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
void floyd()
{
    ll dist[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            dist[i][j]=arr[i][j];
        }
    }
    for(int k=n-1;k>=0;k--)
    {
        //cout<<"x[k] "<<v1[k]<<endl;
        mp[v1[k]]=1;
        int suma=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                dist[i][j]=min(dist[i][j],dist[i][v1[k]]+dist[v1[k]][j]);

            }

        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(mp[i]==1 and mp[j]==1)
                {
                    suma+=dist[i][j];
                }
            }
        }
       // cout<<suma<<endl;
        ans.pb(suma);
    }
    //reverse(ans.begin(),ans.end());
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    while(t--)
    {
        cin>>n;
        //cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }

        for(int i=0;i<n;i++)
        cin>>v1[i],v1[i]-=1;

        //cout<<endl;
        floyd();
        reverse(ans.begin(),ans.end());
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
ll arr[501][501];
vector<int>D;
int x;
ll t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
int floyd()
{
    ll dist[n][n];
    for(int i=0;i<D.size();i++)
    {
        for(int j=0;j<D.size();j++)
        dist[D[i]][D[j]]=arr[D[i]][D[j]];
    }
    for(int k=0;k<D.size();k++)
    {
        for(int i=0;i<D.size();i++)
        {
            for(int j=0;j<D.size();j++)
            {
                if(dist[D[i]][D[j]]>dist[D[i]][D[k]]+dist[D[k]][D[j]])
                {
                    dist[D[i]][D[j]]=dist[D[i]][D[k]]+dist[D[k]][D[j]];
                }
            }

        }
    }
    int suma=0;
    for(int i=0;i<D.size();i++)
    {
        for(int j=0;j<D.size();j++)
        {
            suma+=dist[D[i]][D[j]];
        }
    }
    return suma;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    ll u,v;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }
        int x;
        vector<int>ans;
        //cout<<INF<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(mp[j]==0)
                D.pb(j);
            }
            ans.pb(floyd());
            cin>>x;
            x-=1;
            mp[x]=1;
            for(int i=0;i<n;i++)
            {
                arr[i][x]=INF;
                arr[x][i]=INF;
            }
            D.clear();
        }
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
ll arr[501][501];
vector<int>D;
int x;
ll t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
int floyd()
{
    ll dist[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        dist[i][j]=arr[i][j];
    }
    for(int k=0;k<n;k++)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(dist[i][j]>dist[i][k]+dist[k][j])
                {
                    dist[i][j]=dist[i][k]+dist[k][j];
                }
            }

        }
    }
    int suma=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(mp[i]==0 and mp[j]==0)
                suma+=dist[i][j];
        }
    }
    return suma;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    ll u,v;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }
        int x;
        vector<int>ans;
        //cout<<INF<<endl;
        for(int i=0;i<n;i++)
        {

            ans.pb(floyd());
            cin>>x;
            x-=1;
            mp[x]=1;
            for(int i=0;i<n;i++)
            {
                arr[i][x]=INF;
                arr[x][i]=INF;
            }
        }
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define F first
#define S second
# define INF int(1e9)
vector<ll>adj[int(2e5)+1];
vector<ll>a(int(2e5)+1);
vector<ll>visited(int(2e5)+1,0);
vector<int>d1(int(2e5)+1,int(2e5)+10);
vector<int>d2(int(2e5)+1,int(2e5)+10);
ll arr[501][501];
vector<int>D;
vector<int>v1(501);
vector<ll>ans;
ll t,n,m,k;
priority_queue<pair<int,int>>pq;
priority_queue<pair<int,int>>pq1;
/*void reset()
{

    while (!pq1.empty())
    pq1.pop();
}
*/
unordered_map<int,int>mp;
unordered_map<int,unordered_map<int,int>>mp1;
void dfs(int u)
{
    visited[u]=1;
    //cout<<u<<endl;
    for(auto it:adj[u])
    {
        if(a[u-1]!=a[it-1])
        mp1[a[u-1]][a[it-1]]=1;
    }
    /*for(auto it:mp1)
    cout<<it.F<<" ";
    cout<<endl;
    */

    for(auto it:adj[u])
    {
        if(visited[it]==0)
        {
            dfs(it);
        }
    }

}
void floyd()
{
    ll dist[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            dist[i][j]=arr[i][j];
        }
    }
    vector<ll>check(n+1,0);
    for(int k=n-1;k>=0;k--)
    {
        //cout<<"x[k] "<<v1[k]<<endl;
        check[v1[k]]=1;
        ll suma=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                dist[i][j]=min(dist[i][j],dist[i][v1[k]]+dist[v1[k]][j]);

            }

        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(check[i] and check[j])
                {
                    suma+=dist[i][j];
                }
            }
        }
       // cout<<suma<<endl;
        ans.pb(suma);
    }
    //reverse(ans.begin(),ans.end());
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    while(t--)
    {
        cin>>n;
        //cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            cin>>arr[i][j];
        }

        for(int i=0;i<n;i++)
        cin>>v1[i],v1[i]-=1;

        //cout<<endl;
        floyd();
        reverse(ans.begin(),ans.end());
        for(auto it:ans)
        cout<<it<<" ";
        cout<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
vector<ll> v;
void dijkstra(ll u)
{
	ll i,j,k;

    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
        if(d[u][i]+d[i][j]<d[u][j])
        d[u][j]=d[u][i]+d[i][j];
        if(d[j][i]+d[i][u]<d[j][u])
        d[j][u]=d[j][i]+d[i][u];
    }
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    if(j!=i)
    {
        if(d[i][u]+d[u][j]<d[i][j])
        d[i][j]=d[i][u]+d[u][j];
        if(d[j][u]+d[u][i]<d[j][i])
       d[j][i]=d[j][u]+d[u][i];
    }
	for(i=0;i<v.size();i++)
		for(j=0;j<v.size();j++)
			sum=sum+d[v[i]][v[j]];

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	v.pb(a[n]);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=min(g[a[i]][a[j]],d[a[i]][a[j]]);
				d[a[j]][a[i]]=min(g[a[j]][a[i]],d[a[j]][a[i]]);
			}
			sum=0;
			v.pb(a[i]);
		dijkstra(a[i]);
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
vector<ll> v;
void dijkstra(ll u)
{
	ll i,j,k;

    for(i=0;i<v.size();i++)
    for(j=0;j<v.size();j++)
    {
        if(d[u][v[i]]+d[v[i]][v[j]]<d[u][v[j]])
        d[u][v[j]]=d[u][v[i]]+d[v[i]][v[j]];
        if(d[v[j]][v[i]]+d[v[i]][u]<d[v[j]][u])
        d[v[j]][u]=d[v[j]][v[i]]+d[v[i]][u];
    }
    for(i=0;i<v.size();i++)
    for(j=0;j<v.size();j++)
    {
        if(d[v[i]][u]+d[u][v[j]]<d[v[i]][v[j]])
        d[v[i]][v[j]]=d[v[i]][u]+d[u][v[j]];
        if(d[v[j]][u]+d[u][v[i]]<d[v[j]][v[i]])
       d[v[j]][v[i]]=d[v[j]][u]+d[u][v[i]];
    }
	for(i=0;i<v.size();i++)
		for(j=0;j<v.size();j++)
			sum=sum+d[v[i]][v[j]];

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	v.pb(a[n]);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=min(g[a[i]][a[j]],d[a[i]][a[j]]);
				d[a[j]][a[i]]=min(g[a[j]][a[i]],d[a[j]][a[i]]);
			}
			sum=0;
			v.pb(a[i]);
		dijkstra(a[i]);
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
void dijkstra(ll u)
{
	ll i,j,k;
    for(i=1;i<=n;i++)
    if(visited[i])
    for(j=1;j<=n;j++)
    if(visited[j] && j!=i)
    {
        if(d[i][u]+d[u][j]<d[i][j])
        d[i][j]=d[i][u]+d[u][j];
        if(d[j][u]+d[u][i]<d[j][i])
        d[j][i]=d[j][u]+d[u][i];
    }

	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		if(visited[i] && visited[j])
			sum=sum+d[i][j];

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=g[a[i]][a[j]];
				d[a[j]][a[i]]=g[a[j]][a[i]];
			}
			sum=0;
		dijkstra(a[i]);
	//	cout<<sum<<" ";
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
vector<ll> v;
void dijkstra()
{
	ll i,j,k;
    for(k=0;k<v.size();k++)
    	for(i=0;i<v.size();i++)
    		for(j=0;j<v.size();j++)
    			if(j!=i)
    			{
    				if(d[v[i]][v[k]]+d[v[k]][v[j]]<d[v[i]][v[j]])
    					d[v[i]][v[j]]=d[v[i]][v[k]]+d[v[k]][v[j]];
    				if(d[v[j]][v[k]]+d[v[k]][v[i]]<d[v[j]][v[i]])
    					d[v[j]][v[i]]=d[v[j]][v[k]]+d[v[k]][v[i]];
    			}
    for(i=0;i<v.size();i++)
    	for(j=0;j<v.size();j++)
    		sum=sum+d[v[i]][v[j]];
}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	visited[a[n]]=1;
	v.pb(a[n]);
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=g[a[i]][a[j]];
				d[a[j]][a[i]]=g[a[j]][a[i]];
			}
			v.pb(a[i]);
			sum=0;
		dijkstra();
	//	cout<<sum<<" ";
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
void dijkstra(ll u)
{
	ll i,j,k;
    for(k=1;k<=n;k++)
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
        if(d[i][k]+d[k][j]<d[i][j])
        d[i][j]=d[i][k]+d[k][j];
        if(d[j][k]+d[k][i]<d[j][i])
        d[j][i]=d[j][k]+d[k][i];
    }

	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		if(visited[i] && visited[j])
			sum=sum+d[i][j];

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=g[a[i]][a[j]];
				d[a[j]][a[i]]=g[a[j]][a[i]];
			}
			sum=0;
		dijkstra(a[i]);
	//	cout<<sum<<" ";
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
void dijkstra(ll u)
{
	ll i,j,k;
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    if(j!=i)
    {
        if(d[i][u]+d[u][j]<d[i][j])
        d[i][j]=d[i][u]+d[u][j];
        if(d[j][u]+d[u][i]<d[j][i])
       d[j][i]=d[j][u]+d[u][i];
    }
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
        if(d[u][i]+d[i][j]<d[u][j])
        d[u][j]=d[u][i]+d[i][j];
        if(d[j][i]+d[i][u]<d[j][u])
        d[j][u]=d[j][i]+d[i][u];
    }

	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		if(d[i][j]!=1e18)
		{

			sum=sum+d[i][j];
		}

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=min(g[a[i]][a[j]],d[a[i]][a[j]]);
				d[a[j]][a[i]]=min(g[a[j]][a[i]],d[a[j]][a[i]]);
			}
			sum=0;
		dijkstra(a[i]);
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
void dijkstra(ll u)
{
	ll i,j,k;
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    if(j!=i)
    {
        if(d[i][u]+d[u][j]<d[i][j])
        d[i][j]=d[i][u]+d[u][j];
        if(d[j][u]+d[u][i]<d[j][i])
       d[j][i]=d[j][u]+d[u][i];
    }
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
        if(d[u][i]+d[i][j]<d[u][j])
        d[u][j]=d[u][i]+d[i][j];
        if(d[j][i]+d[i][u]<d[j][u])
        d[j][u]=d[j][i]+d[i][u];
    }
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    if(j!=i)
    {
        if(d[i][u]+d[u][j]<d[i][j])
        d[i][j]=d[i][u]+d[u][j];
        if(d[j][u]+d[u][i]<d[j][i])
       d[j][i]=d[j][u]+d[u][i];
    }
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		if(d[i][j]!=1e18)
		{

			sum=sum+d[i][j];
		}

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=min(g[a[i]][a[j]],d[a[i]][a[j]]);
				d[a[j]][a[i]]=min(g[a[j]][a[i]],d[a[j]][a[i]]);
			}
			sum=0;
		dijkstra(a[i]);
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define cbrt cbrtl
using namespace std;
ll n,d[505][505],a[505],g[505][505],sum;
ll visited[505];
void dijkstra(ll u)
{
	ll i,j,k;
    for(k=1;k<=n;k++)
    if(visited[k])
    for(i=1;i<=n;i++)
    if(visited[i])
    for(j=1;j<=n;j++)
    if(visited[j] && j!=i)
    {
        if(d[i][k]+d[k][j]<d[i][j])
        d[i][j]=d[i][k]+d[k][j];
        if(d[j][k]+d[k][i]<d[j][i])
        d[j][i]=d[j][k]+d[k][i];
    }

	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		if(visited[i] && visited[j])
			sum=sum+d[i][j];

}
int32_t main()
{
	IOS
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			{
				cin>>g[i][j];
				d[i][j]=1e18;
			}
	vector<ll> ans;
	for(i=1;i<=n;i++)
		{
			cin>>a[i];
			d[i][i]=0;
		}
	ans.pb(0);
	visited[a[n]]=1;
	for(i=n-1;i>=1;i--)
	{
	    visited[a[i]]=1;
		for(j=i+1;j<=n;j++)
			{
				d[a[i]][a[j]]=g[a[i]][a[j]];
				d[a[j]][a[i]]=g[a[j]][a[i]];
			}
			sum=0;
		dijkstra(a[i]);
	//	cout<<sum<<" ";
		ans.pb(sum);
	}
	for(i=n-1;i>=0;i--)
		cout<<ans[i]<<" ";
	return 0;
}


 #include<bits/stdc++.h>
using  namespace std;

#define  ll                       long long
#define  pb                       push_back
#define  lb                       lower_bound
#define  ub                       upper_bound
#define  fr                       first
#define  sc                       second
#define  memset( a, b )           memset( a, b , sizeof(a) )
#define  mod                        1000000007
#define  rep( i , n )            for( i=0 ; i<n; i++ )
#define  INF                      1000000000000000000
#define FAST                     ios_base::sync_with_stdio (false), cin.tie(nullptr );
#define  N                        200003

  ll t ,d , ct  , q, ans ,l ,m , r ,n , mx, mn , i, j, k ,x , y , z , s  ;

  char ch;           string ss = "" ,  str = "" ;

  map<ll,ll>mp   ;     set< ll >st ;

  ll a[N] ,  b[N] ;

     /*  -------------------------------Main Code------------------------------- */

int main(){       FAST

 //cin>>t ; while( t-- ) {

      s = k = d = l  = mx =m = mn = r= q =i=j = z= x= y =ct =ans = 0;

cin >> n ;

int dist[n+2][n+2] ;

for(i=1 ; i<=n  ;i++)
for(j=1; j<=n ; j++)
cin >>dist[i][j] ;
vector<int>res ;
bool vis[n+2] ;

memset(vis ,0) ;

rep(i ,n) cin >> a[i+1] ;

for(l =n ; l >=1 ; l-- )
{
s =0 ;
k = a[l] ;
vis[k] = 1 ;

for(i=1 ; i<=n ; i++)
for(j=1; j<=n ; j++ )
if(vis[i] && vis[j]) dist[i][j] = min( dist[i][j]  ,
  dist[i][k]+dist[k][j] ) ,
  s += dist[i][j] ;

res.pb(s) ;
}

for(i=res.size() -1 ; i>=0; i--)
 cout << res[i] <<" " ;

return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=505;

int n;
int a[N], ans[N], vis[N];
int dist[N][N];

int32_t main()
{
	IOS;
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			cin>>dist[i][j];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int idx=n;idx>=1;idx--)
	{
		int k=a[idx];
		vis[k]=1;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				dist[i][j]=min(dist[i][j], dist[i][k] + dist[k][j]);
				if(vis[i] && vis[j])
					ans[idx]+=dist[i][j];
			}
		}
	}
	for(int i=1;i<=n;i++)
		cout<<ans[i]<<" ";
	return 0;
}


 #include<bits/stdc++.h>
using  namespace std;

#define  ll                       long long
#define  pb                       push_back
#define  lb                       lower_bound
#define  ub                       upper_bound
#define  fr                       first
#define  sc                       second
#define  memset( a, b )           memset( a, b , sizeof(a) )
#define  mod                        1000000007
#define  rep( i , n )            for( i=0 ; i<n; i++ )
#define  INF                      1000000000000000000
#define FAST                     ios_base::sync_with_stdio (false), cin.tie(nullptr );
#define  N                        200003

  ll t ,d , ct  , q, ans ,l ,m , r ,n , mx, mn , i, j, k ,x , y , z , s  ;

  char ch;           string ss = "" ,  str = "" ;

  map<ll,ll>mp   ;     set< ll >st ;

  ll a[N] ,  b[N] ;

     /*  -------------------------------Main Code------------------------------- */

int main(){       FAST

 //cin>>t ; while( t-- ) {

      s = k = d = l  = mx =m = mn = r= q =i=j = z= x= y =ct =ans = 0;

cin >> n ;

int dist[n+2][n+2] ;

for(i=1 ; i<=n  ;i++)
for(j=1; j<=n ; j++)
cin >>dist[i][j] ;

vector<int>res ;
bool vis[n+2] ;

memset(vis ,0) ;

rep(i ,n) cin >> a[i+1] ;

for(l =n ; l >=1 ; l-- )
{
s =0 ;
k = a[l] ;
vis[k] = 1 ;

for(i=1 ; i<=n ; i++)
for(j=1; j<=n ; j++ )
{dist[i][j] = min( dist[i][j]  ,
  dist[i][k]+dist[k][j] ) ;

  if(vis[i] && vis[j])
  s += dist[i][j] ;
}
res.pb(s) ;
}

for(i=res.size() -1 ; i>=0; i--)
 cout << res[i] <<" " ;

return 0;
}


 #include<bits/stdc++.h>
using  namespace std;

#define  ll                       long long
#define  pb                       push_back
#define  lb                       lower_bound
#define  ub                       upper_bound
#define  fr                       first
#define  sc                       second
#define  memset( a, b )           memset( a, b , sizeof(a) )
#define  mod                        1000000007
#define  rep( i , n )            for( i=0 ; i<n; i++ )
#define  INF                      1000000000000000000
#define FAST                     ios_base::sync_with_stdio (false), cin.tie(nullptr );
#define  N                        200003

  ll t ,d , ct  , q, ans ,l ,m , r ,n , mx, mn , i, j, k ,x , y , z , s  ;

  char ch;           string ss = "" ,  str = "" ;

  map<ll,ll>mp   ;     set< ll >st ;

  ll a[N] ,  b[N] ;

     /*  -------------------------------Main Code------------------------------- */

int main(){       FAST

 //cin>>t ; while( t-- ) {

      s = k = d = l  = mx =m = mn = r= q =i=j = z= x= y =ct =ans = 0;

cin >> n ;

int dist[n+2][n+2] ;

for(i=1 ; i<=n  ;i++)
for(j=1; j<=n ; j++)
cin >>dist[i][j] ;

vector<ll>res ;
bool vis[n+2] ;

memset(vis ,0) ;

rep(i ,n) cin >> a[i+1] ;

for(l =n ; l >=1 ; l-- )
{
ll cost =0 ;
k = a[l] ;
vis[k] = 1 ;

for(i=1 ; i<=n ; i++)
for(j=1; j<=n ; j++ )
{dist[i][j] = min( dist[i][j]  ,
  dist[i][k]+dist[k][j] ) ;

  if(vis[i] && vis[j])
  cost += dist[i][j] ;
}
res.pb(cost) ;
}

for(i=res.size() -1 ; i>=0; i--)
 cout << res[i] <<" " ;

return 0;
}


 #include<bits/stdc++.h>
using  namespace std;

#define  ll                       long long
#define  pb                       push_back
#define  lb                       lower_bound
#define  ub                       upper_bound
#define  fr                       first
#define  sc                       second
#define  memset( a, b )           memset( a, b , sizeof(a) )
#define  mod                        1000000007
#define  rep( i , n )            for( i=0 ; i<n; i++ )
#define  INF                      1000000000000000000
#define FAST                     ios_base::sync_with_stdio (false), cin.tie(nullptr );
#define  N                        200003

  ll t ,d , ct  , q, ans ,l ,m , r ,n , mx, mn , i, j, k ,x , y , z , s  ;

  char ch;           string ss = "" ,  str = "" ;

  map<ll,ll>mp   ;     set< ll >st ;

  ll a[N] ,  b[N] ;

     /*  -------------------------------Main Code------------------------------- */

int main(){       FAST

 //cin>>t ; while( t-- ) {

      s = k = d = l  = mx =m = mn = r= q =i=j = z= x= y =ct =ans = 0;

cin >> n ;

int dist[n+2][n+2] ;

for(i=1 ; i<=n  ;i++)
for(j=1; j<=n ; j++)
cin >>dist[i][j] ;

vector<int>res ;
bool vis[n+2] ;

memset(vis ,0) ;

rep(i ,n) cin >> a[i+1] ;

for(l =n ; l >=1 ; l-- )
{
int cost =0 ;
k = a[l] ;
vis[k] = 1 ;

for(i=1 ; i<=n ; i++)
for(j=1; j<=n ; j++ )
{dist[i][j] = min( dist[i][j]  ,
  dist[i][k]+dist[k][j] ) ;

  if(vis[i] && vis[j])
  cost += dist[i][j] ;
}
res.pb(cost) ;
}

for(i=res.size() -1 ; i>=0; i--)
 cout << res[i] <<" " ;

return 0;
}


 #include<bits/stdc++.h>
using  namespace std;

#define  ll                       long long
#define  pb                       push_back
#define  lb                       lower_bound
#define  ub                       upper_bound
#define  fr                       first
#define  sc                       second
#define  memset( a, b )           memset( a, b , sizeof(a) )
#define  mod                        1000000007
#define  rep( i , n )            for( i=0 ; i<n; i++ )
#define  INF                      1000000000000000000
#define FAST                     ios_base::sync_with_stdio (false), cin.tie(nullptr );
#define  N                        200003

  int t ,d , ct  , q, ans ,l ,m , r ,n , mx, mn , i, j, k ,x , y , z , s  ;

  char ch;           string ss = "" ,  str = "" ;

  int a[N] ,  b[N] ;

     /*  -------------------------------Main Code------------------------------- */

int main(){       FAST

// cin>>t ; while( t-- ) {

      s = k = d = l  = mx =m = mn = r= q =i=j = z= x= y =ct =ans = 0;

cin >> n ;

int dist[n+2][n+2] ;
int dp[n+2][n+2] ;

for(i=1 ; i<=n ; i++)
{    for(j=1 ; j<=n ; j++)
cin >> dist[i][j] ,
dp[i][j] = dist[i][j] ;

}

rep(i ,n) cin >> a[i+1] ;

vector<int>v ;
v.pb(a[n]) ;

vector<int>res ;
res.pb(0) ;

for(i=n-1 ; i>=1 ; i--)
{
s = 0;
int curr = a[i] ;

set<pair<int,int>>st ;
for(j=0 ; j<v.size() ; j++)
    st.insert({dp[v[j]][curr] , v[j]})  ;

auto it = st.begin() ;
pair<int,int>p = *it ;

vector<int>temp ;
temp.pb(p.sc) ;
it++ ;

for( ; it!=st.end() ; it++){

p = *it ;
 x = p.sc ;
for(j=0; j<temp.size() ; j++)
   {
dp[x][curr] = min(dp[x][curr] , dp[temp[j]][curr] + dp[x][temp[j]] ) ;

   }

temp.pb(x) ;

}
temp.clear() ;
st.clear();

for(j=0 ; j<v.size() ; j++)
    st.insert({dp[curr][v[j]] , v[j]})  ;

 it = st.begin() ;
p = *it ;

//  cout<<p.fr<<" "<<p.fr<<"\n";
temp.pb(p.sc) ;
it++ ;

for( ; it!=st.end() ; it++){

p = *it ;
 x = p.sc ;
for(j=0; j<temp.size() ; j++)
   {
dp[curr][x] = min(dp[curr][x] ,
 dp[curr][temp[j]] + dp[temp[j]][curr] ) ;

   }

temp.pb(x) ;

}

v.pb(curr) ;

for(j=0 ;j<v.size() ; j++)
for(k=j+1 ; k<v.size() ; k++)
{
dp[v[k]][v[j]] = min(dp[v[k]][v[j]] , dp[v[k]][curr] + dp[curr][v[j]] ) ;

dp[v[j]][v[k]] = min(dp[v[j]][v[k]] , dp[v[j]][curr] + dp[curr][v[k]]  );
//cout<<dp[v[k]][v[j]] <<" "<<dp[v[j]][v[k]]<<"\n";
s += dp[v[k]][v[j]] + dp[v[j]][v[k]] ;

}

res.pb(s) ;

}

for(i=res.size()-1 ; i >=0 ; i--)
 cout<<res[i] <<" " ;

return 0;
}


#pragma optimise GCC(-O2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f(a,b,c) for(int a=b;a<c;a++)
#define read(t) ll t; cin>>t;
#define readarr(arr,n) ll arr[n]; f(i,0,n) cin>>arr[i];
#define forstl(i,v) for(auto &i: v)
#define ln endl
#define dbg(x) cout<<#x<<" = "<<x<<ln;
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<ln;
#define dbgstl(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it<<ln;} cout<<ln;
#define dbgstlp(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it.fi<<" , "<<it.se<<ln;} cout<<ln;
#define dbgarr(v,s,e) cout<<#v<<" = "; f(i,s,e) cout<<v[i]<<", "; cout<<ln;
#define addEdge(tr,k) f(i,0,k) { ll x,y; cin>>x>>y, tr[x].push_back(y), tr[y].push_back(x);}

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); }

//														GLOBAL VARS
ll INF =LLONG_MAX;
const ll M= 1000000007;

//														USEFUL FUNCTIONS
ll powm(ll , ll ) ;

ll adj[505][505],d[505][505];

ll solve(ll k)
{
	f(i,0,505)
	f(j,0,505)
	if (d[i][k] < INF && d[k][j] < INF)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	ll mx=0;
	f(i,0,505) f(j,0,505) if(d[i][j]!=INF) mx+=d[i][j];
	return mx;
}

// 														MAIN
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    //It gets easier day by day, but the hard part is to........
    read(n)
	f(i,0,505) f(j,0,505) d[i][j]=INF;
	f(i,0,n) f(j,0,n) {cin>>adj[i][j]; }

	vector<ll> x(n);
	f(i,0,n) {cin>>x[i]; x[i]--;}
	reverse(x.begin(),x.end());
	set<ll> s;
	f(i,0,n)
	{
		ll z=x[i];
		d[z][z]=0;
		for(auto xx:s) {d[xx][z]=adj[xx][z];d[z][xx]=adj[z][xx];}
		s.insert(z);
		x[i]=solve(x[i]);

	}

	reverse(x.begin(),x.end());
	f(i,0,n)
	{
		cout<<x[i]<<" ";
	}

}

//													FUNCTIONS DECLARATIONS
ll powm(ll a, ll b) {
	ll res=1;
	while(b) {
		if(b&1)
			res=(res*a)%M;
		a=(a*a)%M;
		b>>=1;
	}
	return res;
}


#pragma optimise GCC(-O2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f(a,b,c) for(int a=b;a<c;a++)
#define read(t) ll t; cin>>t;
#define readarr(arr,n) ll arr[n]; f(i,0,n) cin>>arr[i];
#define forstl(i,v) for(auto &i: v)
#define ln endl
#define dbg(x) cout<<#x<<" = "<<x<<ln;
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<ln;
#define dbgstl(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it<<ln;} cout<<ln;
#define dbgstlp(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it.fi<<" , "<<it.se<<ln;} cout<<ln;
#define dbgarr(v,s,e) cout<<#v<<" = "; f(i,s,e) cout<<v[i]<<", "; cout<<ln;
#define addEdge(tr,k) f(i,0,k) { ll x,y; cin>>x>>y, tr[x].push_back(y), tr[y].push_back(x);}

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); }

//														GLOBAL VARS
ll INF =LLONG_MAX;
const ll M= 1000000007;

//														USEFUL FUNCTIONS
ll powm(ll , ll ) ;

ll adj[505][505],d[505][505];

ll solve(ll k)
{
	// f(jj,0,7)
	{
	// f(i,0,505) f(j,0,505)
	// if (d[i][k] < INF && d[k][j] < INF)
    // d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	f(i,0,505) f(j,0,505)
	if (d[i][j] < INF && d[j][k] < INF)
	d[i][k] = min(d[i][k], d[i][j] + d[j][k]);
	f(i,0,505) f(j,0,505)
	if (d[k][j] < INF && d[j][i] < INF)
	d[k][i] = min(d[k][i], d[k][j] + d[j][i]);
	f(i,0,505) f(j,0,505)
	if (d[i][k] < INF && d[k][j] < INF)
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	}

	ll mx=0;
	f(i,0,505) f(j,0,505) if(d[i][j]!=INF) mx+=d[i][j];
	return mx;
}

// 														MAIN
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    //It gets easier day by day, but the hard part is to........
    read(n)
	f(i,0,505) f(j,0,505) d[i][j]=INF;
	f(i,0,n) f(j,0,n) {cin>>adj[i][j]; }

	vector<ll> x(n);
	f(i,0,n) {cin>>x[i]; x[i]--;}
	reverse(x.begin(),x.end());
	set<ll> s;
	f(i,0,n)
	{
		ll z=x[i];
		d[z][z]=0;
		for(auto xx:s) {d[xx][z]=min(d[xx][z],adj[xx][z]);d[z][xx]=min(d[z][xx],adj[z][xx]);}
		s.insert(z);
		x[i]=solve(x[i]);

	}

	reverse(x.begin(),x.end());
	f(i,0,n)
	{
		cout<<x[i]<<" ";
	}

}

//													FUNCTIONS DECLARATIONS
ll powm(ll a, ll b) {
	ll res=1;
	while(b) {
		if(b&1)
			res=(res*a)%M;
		a=(a*a)%M;
		b>>=1;
	}
	return res;
}


#pragma optimise GCC(-O2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f(a,b,c) for(int a=b;a<c;a++)
#define read(t) ll t; cin>>t;
#define readarr(arr,n) ll arr[n]; f(i,0,n) cin>>arr[i];
#define forstl(i,v) for(auto &i: v)
#define ln endl
#define dbg(x) cout<<#x<<" = "<<x<<ln;
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<ln;
#define dbgstl(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it<<ln;} cout<<ln;
#define dbgstlp(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it.fi<<" , "<<it.se<<ln;} cout<<ln;
#define dbgarr(v,s,e) cout<<#v<<" = "; f(i,s,e) cout<<v[i]<<", "; cout<<ln;
#define addEdge(tr,k) f(i,0,k) { ll x,y; cin>>x>>y, tr[x].push_back(y), tr[y].push_back(x);}

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); }

//														GLOBAL VARS
ll INF =LLONG_MAX;
const ll M= 1000000007;

//														USEFUL FUNCTIONS
ll powm(ll , ll ) ;

ll adj[505][505],d[505][505];

ll solve(ll k)
{
	f(i,0,505)
	f(j,0,505)
		if (d[i][k] < INF && d[k][j] < INF)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	f(i,0,505) f(j,0,505)
	if (d[i][j] < INF && d[j][k] < INF)
	d[i][k] = min(d[i][k], d[i][j] + d[j][k]);
	f(i,0,505) f(j,0,505)
	if (d[k][j] < INF && d[j][i] < INF)
	d[k][i] = min(d[k][i], d[k][j] + d[j][i]);
	ll mx=0;
	f(i,0,505) f(j,0,505) if(d[i][j]!=INF) mx+=d[i][j];
	return mx;
}

// 														MAIN
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    //It gets easier day by day, but the hard part is to........
    read(n)
	f(i,0,505) f(j,0,505) d[i][j]=INF;
	f(i,0,n) f(j,0,n) {cin>>adj[i][j]; }

	vector<ll> x(n);
	f(i,0,n) {cin>>x[i]; x[i]--;}
	reverse(x.begin(),x.end());
	set<ll> s;
	f(i,0,n)
	{
		ll z=x[i];
		d[z][z]=0;
		for(auto xx:s) {d[xx][z]=min(d[xx][z],adj[xx][z]);d[z][xx]=min(d[z][xx],adj[z][xx]);}
		s.insert(z);
		x[i]=solve(x[i]);

	}

	reverse(x.begin(),x.end());
	f(i,0,n)
	{
		cout<<x[i]<<" ";
	}

}

//													FUNCTIONS DECLARATIONS
ll powm(ll a, ll b) {
	ll res=1;
	while(b) {
		if(b&1)
			res=(res*a)%M;
		a=(a*a)%M;
		b>>=1;
	}
	return res;
}


#pragma optimise GCC(-O2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f(a,b,c) for(int a=b;a<c;a++)
#define read(t) ll t; cin>>t;
#define readarr(arr,n) ll arr[n]; f(i,0,n) cin>>arr[i];
#define forstl(i,v) for(auto &i: v)
#define ln endl
#define dbg(x) cout<<#x<<" = "<<x<<ln;
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<ln;
#define dbgstl(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it<<ln;} cout<<ln;
#define dbgstlp(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it.fi<<" , "<<it.se<<ln;} cout<<ln;
#define dbgarr(v,s,e) cout<<#v<<" = "; f(i,s,e) cout<<v[i]<<", "; cout<<ln;
#define addEdge(tr,k) f(i,0,k) { ll x,y; cin>>x>>y, tr[x].push_back(y), tr[y].push_back(x);}

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); }

//														GLOBAL VARS
ll INF =LLONG_MAX;
const ll M= 1000000007;

//														USEFUL FUNCTIONS
ll powm(ll , ll ) ;

ll adj[505][505],d[505][505];

ll solve(ll k)
{
	f(jj,0,2)
	{f(i,0,505) f(j,0,505)
	if (d[i][k] < INF && d[k][j] < INF)
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	f(i,0,505) f(j,0,505)
	if (d[i][j] < INF && d[j][k] < INF)
	d[i][k] = min(d[i][k], d[i][j] + d[j][k]);
	f(i,0,505) f(j,0,505)
	if (d[k][j] < INF && d[j][i] < INF)
	d[k][i] = min(d[k][i], d[k][j] + d[j][i]);}

	ll mx=0;
	f(i,0,505) f(j,0,505) if(d[i][j]!=INF) mx+=d[i][j];
	return mx;
}

// 														MAIN
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    //It gets easier day by day, but the hard part is to........
    read(n)
	f(i,0,505) f(j,0,505) d[i][j]=INF;
	f(i,0,n) f(j,0,n) {cin>>adj[i][j]; }

	vector<ll> x(n);
	f(i,0,n) {cin>>x[i]; x[i]--;}
	reverse(x.begin(),x.end());
	set<ll> s;
	f(i,0,n)
	{
		ll z=x[i];
		d[z][z]=0;
		for(auto xx:s) {d[xx][z]=min(d[xx][z],adj[xx][z]);d[z][xx]=min(d[z][xx],adj[z][xx]);}
		s.insert(z);
		x[i]=solve(x[i]);

	}

	reverse(x.begin(),x.end());
	f(i,0,n)
	{
		cout<<x[i]<<" ";
	}

}

//													FUNCTIONS DECLARATIONS
ll powm(ll a, ll b) {
	ll res=1;
	while(b) {
		if(b&1)
			res=(res*a)%M;
		a=(a*a)%M;
		b>>=1;
	}
	return res;
}


#pragma optimise GCC(-O2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f(a,b,c) for(int a=b;a<c;a++)
#define read(t) ll t; cin>>t;
#define readarr(arr,n) ll arr[n]; f(i,0,n) cin>>arr[i];
#define forstl(i,v) for(auto &i: v)
#define ln endl
#define dbg(x) cout<<#x<<" = "<<x<<ln;
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<ln;
#define dbgstl(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it<<ln;} cout<<ln;
#define dbgstlp(v) cout<<#v<<" = \n"; { int c=0; forstl(it,v) \
cout<<"    Term "<< ++c <<" = "<<it.fi<<" , "<<it.se<<ln;} cout<<ln;
#define dbgarr(v,s,e) cout<<#v<<" = "; f(i,s,e) cout<<v[i]<<", "; cout<<ln;
#define addEdge(tr,k) f(i,0,k) { ll x,y; cin>>x>>y, tr[x].push_back(y), tr[y].push_back(x);}

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); }

//														GLOBAL VARS
ll INF =LLONG_MAX;
const ll M= 1000000007;

//														USEFUL FUNCTIONS
ll powm(ll , ll ) ;

ll adj[505][505],d[505][505];

ll solve(ll k)
{
	// f(jj,0,7)
	{f(i,0,505) f(j,0,505)
	if (d[i][k] < INF && d[k][j] < INF)
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	f(i,0,505) f(j,0,505)
	if (d[i][j] < INF && d[j][k] < INF)
	d[i][k] = min(d[i][k], d[i][j] + d[j][k]);
	f(i,0,505) f(j,0,505)
	if (d[k][j] < INF && d[j][i] < INF)
	d[k][i] = min(d[k][i], d[k][j] + d[j][i]);
	f(i,0,505) f(j,0,505)
	if (d[i][k] < INF && d[k][j] < INF)
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	}

	ll mx=0;
	f(i,0,505) f(j,0,505) if(d[i][j]!=INF) mx+=d[i][j];
	return mx;
}

// 														MAIN
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    //It gets easier day by day, but the hard part is to........
    read(n)
	f(i,0,505) f(j,0,505) d[i][j]=INF;
	f(i,0,n) f(j,0,n) {cin>>adj[i][j]; }

	vector<ll> x(n);
	f(i,0,n) {cin>>x[i]; x[i]--;}
	reverse(x.begin(),x.end());
	set<ll> s;
	f(i,0,n)
	{
		ll z=x[i];
		d[z][z]=0;
		for(auto xx:s) {d[xx][z]=min(d[xx][z],adj[xx][z]);d[z][xx]=min(d[z][xx],adj[z][xx]);}
		s.insert(z);
		x[i]=solve(x[i]);

	}

	reverse(x.begin(),x.end());
	f(i,0,n)
	{
		cout<<x[i]<<" ";
	}

}

//													FUNCTIONS DECLARATIONS
ll powm(ll a, ll b) {
	ll res=1;
	while(b) {
		if(b&1)
			res=(res*a)%M;
		a=(a*a)%M;
		b>>=1;
	}
	return res;
}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

void floyd(int k, vector<vector<int>> &g)
{

        for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(g[i][j] > g[i][k] + g[k][j])
                                        g[i][j] = g[i][k] + g[k][j];
                        }
                }
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n, vector<int> (n));
        vector<int> v(n);
        vector<vector<int>> wt(n, vector<int> (n));
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = 0; i < n; i++)
        {
                cin >> v[i];
        }

        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        wt[i][j] = inf;

                }
        }
        set<int> seenvertices;
        for(int e = n-1; e >= 0; e--)
        {
                seenvertices.insert(v[e]-1);
                for(i = 0; i < n; i++)
                {
                        for(j = 0; j < n; j++)
                        {
                                if(seenvertices.count(i) && seenvertices.count(j))
                                        wt[i][j] = g[i][j];
                        }
                }
                floyd(v[e]-1, wt);

                sum = 0;
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(wt[i][j] < 1e7)
                                        sum += wt[i][j];
                        }
                }
                cout << sum << " ";
        }

}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

void floyd(set<int> seen, vector<vector<int>> &g)
{
        for(int k = *seen.begin(); k < *seen.end(); k++)
        {
                for(int i = *seen.begin(); i < *seen.end(); i++)
                {
                        for(int j = *seen.begin(); j < *seen.end(); j++)
                        {
                                if(g[i][j] > g[i][k] + g[k][j])
                                        g[i][j] = g[i][k] + g[k][j];
                        }
                }
        }
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n, vector<int> (n));
        vector<int> v(n);
        vector<vector<int>> wt(n, vector<int> (n));
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = 0; i < n; i++)
        {
                cin >> v[i];
        }

        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        wt[i][j] = inf;

                }
        }
        set<int> seenvertices;
        stack<int> st;
        for(int e = n-1; e >= 0; e--)
        {
                seenvertices.insert(v[e]-1);

                for(i = 0; i < n; i++)
                {
                        for(j = 0; j < n; j++)
                        {
                                if(seenvertices.count(i) && seenvertices.count(j))
                                        wt[i][j] = g[i][j];
                        }
                }

                floyd(seenvertices, wt);
                sum = 0;
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(wt[i][j] < 1e7)
                                        sum += wt[i][j];
                        }
                }
                st.push(sum);
        }
        while(!st.empty())
        {
                cout << st.top() << " ";
                st.pop();
        }
}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n + 1, vector<int> (n + 1));
        vector<int> del(n + 1), ans(n + 1), f(n+1, 0);

        for(i = 1; i <= n; i++)
        {
                for(j = 1; j <= n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = n; i >= 1; i--)
        {
                cin >> del[i];
        }

        for(int x = 1; x <= n; x++)
        {
                ans[x] = 0;
                int k = del[x];
                f[k] = 1;
                for(int i = 1; i <= n; i++)
                {
                        for(int j = 1; j <= n; j++)
                        {
                                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
                                if(f[i] && f[j])
                                        ans[x] += g[i][j];
                        }
                }
        }
        for(int i = n; i >= 1; i--)
                cout << ans[i] << " ";
}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

void floyd(int k, vector<vector<int>> &g)
{

        for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(g[i][j] > g[i][k] + g[k][j])
                                        g[i][j] = g[i][k] + g[k][j];
                        }
                }
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n, vector<int> (n));
        vector<int> v(n);
        vector<vector<int>> wt(n, vector<int> (n));
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = 0; i < n; i++)
        {
                cin >> v[i];
        }

        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        wt[i][j] = inf;

                }
        }
        set<int> seenvertices;
        stack<int> st;
        for(int e = n-1; e >= 0; e--)
        {
                seenvertices.insert(v[e]-1);

                for(i = 0; i < n; i++)
                {
                        for(j = 0; j < n; j++)
                        {
                                if(seenvertices.count(i) && seenvertices.count(j))
                                        wt[i][j] = g[i][j];
                        }
                }

                floyd(v[e]-1, wt);
                sum = 0;
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(wt[i][j] < 1e7)
                                        sum += wt[i][j];
                        }
                }
                st.push(sum);
        }
        while(!st.empty())
        {
                cout << st.top() << " ";
                st.pop();
        }
}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n + 1, vector<int> (n + 1));
        vector<ll> del(n + 1), ans(n + 1), f(n+1, 0);

        for(i = 1; i <= n; i++)
        {
                for(j = 1; j <= n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = n; i >= 1; i--)
        {
                cin >> del[i];
        }

        for(int x = 1; x <= n; x++)
        {
                ans[x] = 0;
                int k = del[x];
                f[k] = 1;
                for(int i = 1; i <= n; i++)
                {
                        for(int j = 1; j <= n; j++)
                        {
                                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
                                if(f[i] && f[j])
                                        ans[x] += g[i][j];
                        }
                }
        }
        for(int i = n; i >= 1; i--)
                cout << ans[i] << " ";
}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

void floyd(set<int> seen, vector<vector<int>> &g)
{
        for(int k = *seen.begin(); k < *seen.end(); k++)
        {
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(g[i][j] > g[i][k] + g[k][j])
                                        g[i][j] = g[i][k] + g[k][j];
                        }
                }
        }
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n, vector<int> (n));
        vector<int> v(n);
        vector<vector<int>> wt(n, vector<int> (n));
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = 0; i < n; i++)
        {
                cin >> v[i];
        }

        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        wt[i][j] = inf;

                }
        }
        set<int> seenvertices;
        stack<int> st;
        for(int e = n-1; e >= 0; e--)
        {
                seenvertices.insert(v[e]-1);

                for(i = 0; i < n; i++)
                {
                        for(j = 0; j < n; j++)
                        {
                                if(seenvertices.count(i) && seenvertices.count(j))
                                        wt[i][j] = g[i][j];
                        }
                }

                floyd(seenvertices, wt);
                sum = 0;
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(wt[i][j] < 1e7)
                                        sum += wt[i][j];
                        }
                }
                st.push(sum);
        }
        while(!st.empty())
        {
                cout << st.top() << " ";
                st.pop();
        }
}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

void floyd(vector<vector<int>> g)
{
        for(int k = 0; k < n; k++)
        {
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(g[i][j] > g[i][k] + g[k][j])
                                        g[i][j] = g[i][k] + g[k][j];
                        }
                }
        }

        int sum = 0;
        for(int i = 0; i < n; i++)
        {
                for(int j = 0; j < n; j++)
                {
                        if(g[i][j] < 1e7)
                                sum += g[i][j];
                       //cout << g[i][j] << " ";
                }
                //cout << endl;
        }
        cout << sum << " ";
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n, vector<int> (n));
        vector<int> v(n);
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = 0; i < n; i++)
        {
                cin >> v[i];
        }
        floyd(g);

        for(int p = 0; p < n-1; p++)
        {
                for(i = 0; i < n; i++)
                {
                        for(j = 0; j < n; j++)
                        {
                                if(i == v[p]-1 || j == v[p]-1)
                                {
                                        g[i][j] = inf;
                                }
                        }
                }
                floyd(g);
        }

}


#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
#define ll long long int
#define inf 1e7;
int n;
vector<vector<int>> g;

void floyd(vector<vector<int>> &g)
{
        for(int k = 0; k < n; k++)
        {
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(g[i][j] > g[i][k] + g[k][j])
                                        g[i][j] = g[i][k] + g[k][j];
                        }
                }
        }
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif

        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll  nn, i, j = 0, nm = 0, cou = 0, sum = 0;
        ll mx = INT_MIN, mn = INT_MAX, lz=0, rz=0, lo=0, ro=INT_MAX, mi = 0;
        ll mid, k;
        ll x, y, z = 0, merged = 0, add = 0;
        ll  a = 0, q, ff = 0, found = 0, p = 0, avg, ind = 0, cnt = 0, t = 0;
        cin >> n;
        g.resize(n, vector<int> (n));
        vector<int> v(n);
        vector<vector<int>> wt(n, vector<int> (n));
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        cin >> g[i][j];
                }
        }
        for(i = 0; i < n; i++)
        {
                cin >> v[i];
        }

        for(i = 0; i < n; i++)
        {
                for(j = 0; j < n; j++)
                {
                        wt[i][j] = inf;

                }
        }
        set<int> seenvertices;
        stack<int> st;
        for(int e = n-1; e >= 0; e--)
        {
                seenvertices.insert(v[e]-1);

                for(i = 0; i < n; i++)
                {
                        for(j = 0; j < n; j++)
                        {
                                if(seenvertices.count(i) && seenvertices.count(j))
                                        wt[i][j] = g[i][j];
                        }
                }

                floyd(wt);
                sum = 0;
                for(int i = 0; i < n; i++)
                {
                        for(int j = 0; j < n; j++)
                        {
                                if(wt[i][j] < 1e7)
                                        sum += wt[i][j];
                        }
                }
                st.push(sum);
        }
        while(!st.empty())
        {
                cout << st.top() << " ";
                st.pop();
        }
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

int a[505][505];
int q[505];

void solve(){
    int n, res = 0;
    cin>>n ;
    rep(i,1,n) rep(j,1,n) cin>>a[i][j];
	rep(i,1,n) cin>>q[i];
    reverse(q+1,q+n+1);
	stack<int> st;
    rep(k,1,n){
    	int sum = 0;
    	rep(i,1,n){
    		rep(j,1,n){
    			min_self(a[q[i]][q[j]], a[q[i]][q[k]]+a[q[k]][q[j]]);
    			if(i<=k && j<=k)sum += a[q[i]][q[j]];
    		}
    	}
    	st.push(sum);
    }

	while(!st.empty()) cout<<st.top()<<' ',st.pop();
	cout<<'\n';

	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

int a[505][505];
int q[505];

void solve(){
    int n, res = 0;
    cin>>n ;
    rep(i,1,n) rep(j,1,n) cin>>a[i][j];
	rep(i,1,n) cin>>q[i];
    reverse(q+1,q+n+1);
	stack<int> st;
    rep(k,1,n){
    	int sum = 0;
    	rep(i,1,n){
    		rep(j,1,n){
    			min_self(a[q[i]][q[j]], a[q[i]][q[k]]+a[q[k]][q[j]]);
    			if(i<=k && j<=k)sum += a[q[i]][q[j]];
    		}
    	}
    	st.push(sum);
    }

	while(!st.empty()) cout<<st.top()<<' ',st.pop();
	cout<<'\n';

	return;
}
int32_t main()
{
	// ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

void solve(){
    int n, res = 0;
    cin>>n ;
    int a[n+1][n+1],dp[n+1][n+1];
    rep(i,1,n) rep(j,1,n) cin>>a[i][j];
    mms(dp,-1LL);
    int sum = 0;
    vb present(n+1,false);
    vector<int> ans;
    vi q(n);
    cin>>q;
    per(iq,n-1,0){
    	int x = q[iq];
    	present[x] = true;
    	sum = 0;
    	rep(i,1,n){
    		rep(j,1,n){
				if(present[i] && present[j]){
					int t = min(a[i][j], a[i][x]+a[x][j]);
					if(dp[i][j]==-1) dp[i][j] = t;
					else min_self(dp[i][j],t);
					sum += dp[i][j];
				}
    		}
    	}
    	ans.pb(sum);
    }

	per(i,n-1,0) cout<<ans[i]<<' ';
	cout<<'\n';

	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

void solve(){
    int n, res = 0;
    cin>>n ;
    vector<vi> a(n+1,vi(n+1,0));
    rep(i,1,n) rep(j,1,n) cin>>a[i][j];

    vi q(n);
    cin>>q;
    reverse(all(q));
    vi ans;
    rep(k,0,n-1){
    	int sum = 0;
    	rep(i,0,n-1){
    		rep(j,0,n-1){
    			min_self(a[q[i]][q[j]], a[q[i]][q[k]]+a[q[k]][q[j]]);
    			if(i<=k && j<=k)sum += a[q[i]][q[j]];
    		}
    	}
    	ans.pb(sum);
    }

	per(i,n-1,0) cout<<ans[i]<<' ';
	cout<<'\n';

	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

int a[505][505];
int q[505];

void solve(){
    int n, res = 0;
    cin>>n ;
    rep(i,1,n) rep(j,1,n) scanf("%lld", &a[i][j]);
	rep(i,1,n) scanf("%lld", &q[i]);
    reverse(q+1,q+n+1);
	stack<int> st;
    rep(k,1,n){
    	int sum = 0;
    	rep(i,1,n){
    		rep(j,1,n){
    			min_self(a[q[i]][q[j]], a[q[i]][q[k]]+a[q[k]][q[j]]);
    			if(i<=k && j<=k)sum += a[q[i]][q[j]];
    		}
    	}
    	st.push(sum);
    }

	while(!st.empty()) printf("%lld ",st.top()),st.pop();
	printf("\n");

	return;
}
int32_t main()
{
	// ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

void solve(){
    int n, res = 0;
    cin>>n ;
    int a[n+1][n+1],dp[n+1][n+1];
    rep(i,1,n) rep(j,1,n) cin>>a[i][j];
    mms(dp,0LL);
    int sum = 0;
    vb present(n+1,false);
    vector<int> ans;
    vi q(n);
    cin>>q;
    per(iq,n-1,0){
    	int x = q[iq];
    	present[x] = true;
    	sum = 0;
    	rep(i,1,n){
    		rep(j,1,n){
				if(present[i] && present[j]){
					dp[i][j] = min(a[i][j], a[i][x]+a[x][j]);
					sum += dp[i][j];
				}
    		}
    	}
    	ans.pb(sum);
    }

	per(i,n-1,0) cout<<ans[i]<<' ';
	cout<<'\n';

	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : B. Greg and Graph
// Contest : Codeforces - Codeforces Round #179 (Div. 1)
// URL : https://codeforces.com/problemset/problem/295/B
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pair<int,int>>
#define mod 1000000007
#define M_PI 3.14159265358979323846
#define pint pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b; i>=a ;--i)
#define mms(a,n) memset(a,n,sizeof(a))
#define fill(a,value) fill(a.begin(),a.end(),value)
#define all(a) a.begin(),a.end()
#define n_ones(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
#define see(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it){}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
}
template <typename T, typename U>inline void min_self(T &x, U y){if (y < x)x = y;}
template <typename T, typename U>inline void max_self(T &x, U y){if (x < y)x = y;}
template<class T1, class T2>ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<"["<<v.first<<", "<<v.second<<"]"; return out<<")"; }
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
template <class Container>
void split(const std::string& str, Container& cont, char delim = ',')
{
    stringstream ss(str);
    string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
// INT64_MAX
// transform(all(s), s.begin(), ::tolower);

int px[4] = {-1,0,1,0};
int py[4] = {0,-1,0,1};

void solve(){
    int n, res = 0;
    cin>>n ;
    int a[n+1][n+1];
    rep(i,1,n) rep(j,1,n) cin>>a[i][j];

    int q[n+1];
	rep(i,1,n) cin>>q[i];
    reverse(q+1,q+n+1);
    vi ans;
    rep(k,1,n){
    	int sum = 0;
    	rep(i,1,n){
    		rep(j,1,n){
    			min_self(a[q[i]][q[j]], a[q[i]][q[k]]+a[q[k]][q[j]]);
    			if(i<=k && j<=k)sum += a[q[i]][q[j]];
    		}
    	}
    	ans.pb(sum);
    }

	per(i,n-1,0) cout<<ans[i]<<' ';
	cout<<'\n';

	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}