#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,m,i,x;
	long long value[1000001]={0};
	cin>>n>>m;
	for(i=0;i<n;i++)
	{
		cin>>x;
		value[x]=1;
	}
	vector<long long> b;
	i=1;
	while(m)
	{
		if(value[i]==0)
		{
			if(m>=i)
			{
				m=m-i;
				b.push_back(i);

			}
			else
			{
				break;
			}
		}
		i++;
	}
	i=0;
	cout<<b.size()<<endl;
	while(i<b.size())
	{
		cout<<b[i]<<" ";
		i++;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,m,i,x;
	long long value[1000000001]={0};
	cin>>n>>m;
	for(i=0;i<n;i++)
	{
		cin>>x;
		value[x]=1;
	}
	vector<long long> b;
	i=1;
	while(m)
	{
		if(value[i]==0)
		{
			if(m>=i)
			{
				m=m-i;
				b.push_back(i);

			}
			else
			{
				break;
			}
		}
		i++;
	}
	i=0;
	cout<<b.size()<<endl;
	while(i<b.size())
	{
		cout<<b[i]<<" ";
		i++;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
ll a[n];
FOR(i,0,n)
cin>>a[i];
sort(a,a+n);
ll tp[n],k=0,sum=0,i=0;
for( ll p=1;sum<m;p++)
{
if(a[k]!=p)
{tp[i]=p ;sum+=p;i++; }
else
{k++;}
}
if(sum>m)
i--;
cout<<i<<"\n";
FOR(j,0,i)
cout<<tp[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
map<ll,ll> mp;
mp.clear();
ll a[n];
FOR(i,0,n)
{cin>>a[i];mp[a[i]]=1;}
ll k=0,sum=0,l;
vector<ll> v;
for( ll p=1;p<=m;p++)
{
if(mp[p]!=1)
{v.push_back(p) ;m-=p;}

}
cout<<v.size()<<"\n";
FOR(j,0,v.size())
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
ll a[n];
FOR(i,0,n)
cin>>a[i];
ll k=0,sum=0,l;
vector<ll> v;
for( ll p=1;sum<=m;p++)
{
if(a[k]!=p)
{v.push_back(p) ;sum+=p; }
else
{k++;}
}
if(sum>m)
l=v.size()-1;
cout<<l<<"\n";
FOR(j,0,l)
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
map<ll,ll> mp;
mp.clear();
ll a[n];
FOR(i,0,n)
{cin>>a[i];mp[a[i]]=1;}
ll k=0,sum=0,l;
vector<ll> v;
for( ll p=1;sum<=m;p++)
{
if(mp[p]!=1)
{v.push_back(p) ;sum+=p; }
else
{k++;}
}
if(sum>m)
l=v.size()-1;
cout<<l<<"\n";
FOR(j,0,l)
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 1000010
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
 ll v[MAX],vis[MAX];
ll n,m;
cin>>n>>m;
ll a[n];
FOR(i,0,n)
{cin>>a[i];vis[a[i]]=1;}
ll k=0,sum=0,l;

for( ll p=1;p<=m;p++)
{
if(vis[p]!=1)
{v[k++]=p ;m-=p;}

}
cout<<k<<"\n";
FOR(j,0,k)
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 1000010
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
 ll v[MAX],vis[MAX];
int main()
{

ll n,m;
cin>>n>>m;
ll a;
FOR(i,0,n)
{cin>>a;vis[a]=1;}
ll k=0;
for( ll p=1;p<=m;p++)
{
if(vis[p]!=1)
{v[k++]=p ;m-=p;}

}
cout<<k<<"\n";
FOR(j,0,k)
cout<<v[j]<<" ";
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
ll a[n];
FOR(i,0,n)
cin>>a[i];
sort(a,a+n);
ll k=0,sum=0,l;
vector<ll> v;
for( ll p=1;sum<=m;p++)
{
if(a[k]!=p)
{v.push_back(p) ;sum+=p; }
else
{k++;}
}
if(sum>m)
l=v.size()-1;
cout<<l<<"\n";
FOR(j,0,l)
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
map<ll,ll> mp;
mp.clear();
ll a[n];
FOR(i,0,n)
{cin>>a[i];mp[a[i]]=1;}
ll k=0,sum=0,l;
vector<ll> v;
for( ll p=1;p<m;p++)
{
if(mp[p]!=1)
{v.push_back(p) ;m-=p;}

}
cout<<v.size()<<"\n";
FOR(j,0,v.size())
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 1000010
#define ll long
#define FSI ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
    FSI
ll n,m;
cin>>n>>m;
map<ll,ll> mp;
mp.clear();
ll a[n];
FOR(i,0,n)
{cin>>a[i];mp[a[i]]=1;}
ll k=0,sum=0,l;
ll v[MAX];
for( ll p=1;p<=m;p++)
{
if(mp[p]!=1)
{v[k++]=p ;m-=p;}

}
cout<<k<<"\n";
FOR(j,0,k)
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
ll a[n];
FOR(i,0,n)
cin>>a[i];
sort(a,a+n);
ll k=0,sum=0,l;
vector<ll> v;
for( ll p=1;sum<m;p++)
{
if(a[k]!=p)
{v.push_back(p) ;sum+=p; }
else
{k++;}
}
if(sum>m)
l=v.size()-1;
cout<<l<<"\n";
FOR(j,0,l)
cout<<v[j]<<" ";

  }


#include<bits/stdc++.h>
using namespace std;
#define MAX 1000010
#define ll long long
#define FOR(i,a,n) for(ll i=a;i<n;i++)
int main()
{
ll n,m;
cin>>n>>m;
map<ll,ll> mp;
mp.clear();
ll a[n];
FOR(i,0,n)
{cin>>a[i];mp[a[i]]=1;}
ll k=0,sum=0,l;
ll v[MAX];
for( ll p=1;p<=m;p++)
{
if(mp[p]!=1)
{v[k++]=p ;m-=p;}

}
cout<<k<<"\n";
FOR(j,0,k)
cout<<v[j]<<" ";

  }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 int mp[200005];
 map<int,int>mpp;
 int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++){
    int a;
    scanf("%d",&a);
    mp[a]=1;
  }
   for(int i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        mpp[i]=1;
      else break;
    }
   }
   cout<<mpp.size()<<endl;
   for(map<int,int>::iterator itr = mpp.begin();itr!=mpp.end();itr++)
    printf("%d ",itr->first);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 long long mp[200005];
 vector<long long>V;
 int main(){
  long long n,m;
  scanf("%lld %lld",&n,&m);
  for(int i=0;i<n;i++){
    long long a;
    scanf("%lld",&a);
    mp[a]=1;
  }
   for(long long i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        V.push_back(i);
      else break;
    }
   }
   cout<<V.size()<<endl;
   for(int i = 0;i<V.size();i++)
    printf("%d ",V[i]);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 int mp[200000];
 map<int,int>mpp;
 int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++){
    int a;
    scanf("%d",&a);
    mp[a]=1;
  }
   for(int i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        mpp[i]=1;
      else break;
    }
   }
   cout<<mpp.size()<<endl;
   for(map<int,int>::iterator itr = mpp.begin();itr!=mpp.end();itr++)
    printf("%d ",itr->first);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 int mp[200005];
 vector<int>V;
 int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++){
    int a;
    scanf("%d",&a);
    mp[a]=1;
  }
   for(int i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        V.push_back(i);
      else break;
    }
   }
   cout<<V.size()<<endl;
   for(int i = 0;i<V.size();i++)
    printf("%d ",V[i]);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 int mp[200000];
 map<int,int>mpp;
 int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++){
    int a;
    scanf("%d",&a);
    mp[a]=1;
  }
   for(int i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        mpp[i]=1;
      else break;
    }
   }
   cout<<mpp.size()<<endl;
   for(map<int,int>::iterator itr = mpp.begin();itr!=mpp.end();itr++)
    printf("%d ",itr->first);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 long long mp[200005];
 vector<long long>V;
 int main(){
  long long n,m;
  scanf("%lld %lld",&n,&m);
  for(int i=0;i<n;i++){
    long long a;
    scanf("%lld",&a);
    mp[a]=1;
  }
   for(long long i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        V.push_back(i);
      else break;
    }
   }
   cout<<V.size()<<endl;
   for(int i = 0;i<V.size();i++)
    printf("%lld ",V[i]);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 map<int,int>mp;
 vector<long long>V;
 int main(){
  long long n,m;
  scanf("%lld %lld",&n,&m);
  for(int i=0;i<n;i++){
    long long a;
    scanf("%lld",&a);
    mp[a]=1;
  }
   for(long long i=1;i<=200000;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        V.push_back(i);
      else break;
    }
   }
   cout<<V.size()<<endl;
   for(int i = 0;i<V.size();i++)
    printf("%lld ",V[i]);
 }


 ///IN THE NAME OF ALLAH
 #include<bits/stdc++.h>
 using namespace std;
 int mp[200000];
 map<int,int>mpp;
 int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++){
    int a;
    scanf("%d",&a);
    mp[a]=1;
  }
   for(int i=1;i<=100005;i++){
    if(mp[i]==0){
      m-=i;
      if(m>=0)
        mpp[i]=1;
      else break;
    }
   }
   cout<<mpp.size()<<endl;
   for(map<int,int>::iterator itr = mpp.begin();itr!=mpp.end();itr++)
    printf("%d ",itr->first);
 }


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

int main() {
int n,m;
cin>>n>>m;
bool *a=new bool[100001]();
int b;
for(int i=1;i<=n;i++){
	cin>>b;
	a[b]=true;
}
vector<int> v;
int s=0,i,t=0;
for(i=1;s<=m;i++){
	if(i<100000){
	if(a[i]==false){
		if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);
	}}
	else
	if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);
}
cout<<t<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
	return 0;

}


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

int main() {
int n,m;
cin>>n>>m;
int *a=new int[n]();
int b;
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
vector<int> v;
int s=0,i,t=0,p=0;
for(i=1;s<=m;i++){
	if(a[p]==i){
		p++;
	}
	else{
		if(s+i>m)
		break;
		else{
			s+=i;
			t++;
			v.push_back(i);
		}
	}
}
cout<<t<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
	return 0;

}


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

int main() {
int n,m;
cin>>n>>m;
bool *a=new bool[100001]();
int b;
for(int i=1;i<=n;i++){
	cin>>b;
	a[b]=true;
}
vector<int> v;
int s=0,i,t=0;
for(i=1;s<=m;i++){
	if(i<=100000){
	if(a[i]==false){
		if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);
	}}
	else
	{if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);}
}
cout<<t<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
	return 0;

}


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

int main() {
int n,m;
cin>>n>>m;
bool *a=new bool[100001]();
int b;
for(int i=1;i<=n;i++){
	cin>>b;
	a[b]=true;
}
vector<int> v;
int s=0,i,t=0;
for(i=1;s<=m;i++){
	if(i<100000){
	if(a[i]==false){
		if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);
	}}
	else
	{if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);}
}
cout<<t<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
	return 0;

}


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

int main() {
int n,m;
cin>>n>>m;
bool *a=new bool[1000000001]();
int b;
for(int i=1;i<=n;i++){
	cin>>b;
	a[b]=true;
}
vector<int> v;
int s=0,i,t=0;
for(i=1;s<=m;i++){
	if(a[i]==false){
		if(s+i>m)
		break;
		s+=i;
		t++;
		v.push_back(i);
	}
}
cout<<t<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
bool a[100005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[x]=1;
    }
    int sum=0;
    queue<int> q;
    for(int i=1;i<=100000;i++){
        if(a[i]!=0)continue;
        if(sum+i<=m){
            sum+=i;
            q.push_back(i);
        }
        else if(sum-q.back()+i<=m){
            sum-=q.back();
            sum+=i;
            q.pop_back();
            q.push_back(i);
        }
        else{
            break;
        }
    }
    printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.front());
        q.pop_front();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int,bool> a;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[x]=1;
    }
    int sum=0;
    queue<int> q;
    for(int i=1;i<=100000;i++){
        if(a[i]!=0)continue;
        if(sum+i<=m){
            sum+=i;
            q.push(i);
        }
        else{
            break;
        }
    }
    printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.front());
        q.pop();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int,bool> a;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[x]=1;
    }
    ll sum=0;
    queue<int> q;
    for(int i=1;i<=10000000;i++){
        if(a[i]!=0)continue;
        if(sum+i<=m){
            sum+=i;
            q.push(i);
        }
        else{
            break;
        }
    }
    printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.front());
        q.pop();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool a[100005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[x]=1;
    }
    int sum=0;
    queue<int> q;
    for(int i=1;i<=100000;i++){
        if(a[i]!=0)continue;
        if(sum+i<=m){
            sum+=i;
            q.push(i);
        }
        else{
            break;
        }
    }
    printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.front());
        q.pop();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int,bool> a;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[x]=1;
    }
    int sum=0;
    queue<int> q;
    for(int i=1;i<=1000000000;i++){
        if(a[i]!=0)continue;
        if(sum+i<=m){
            sum+=i;
            q.push(i);
        }
        else{
            break;
        }
    }
    printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.front());
        q.pop();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool a[100005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[x]=1;
    }
    int sum=0;
    deque<int> q;
    for(int i=1;i<=100000;i++){
        if(a[i]!=0)continue;
        if(sum+i<=m){
            sum+=i;
            q.push_back(i);
        }
        else if(sum-q.back()+i<=m){
            sum-=q.back();
            sum+=i;
            q.pop_back();
            q.push_back(i);
        }
    }
    printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.front());
        q.pop_front();
    }

    return 0;
}


#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define nav(...) "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
#define oo INT_MAX/2
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
template<class T> struct rge { T b, e; };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
	~debug() {cerr<<endl;}
	template<class T> debug& operator<<(T x) {cerr<<boolalpha<<x; return *this;}
	template<class B, class C> debug& operator<< (pair<B, C> x){return *this<<"("<<x.first<<", "<<x.second<<")";}
	template<class T> debug& operator<<(rge<T> x) {
		*this<<"[";for(auto it=x.b;it!=x.e;it++){*this<<", "+2*(it==x.b)<<*it;}return *this<<"]";}
	template<class T> debug& operator<<(vector<T> x){ return *this<<range(all(x));}
#else
	template<class T> debug& operator <<(const T&) {return *this;}
#endif
};

bool solve(void) {

	int n;
	cin>>n;
	vector<vector<int>> mat(n, vector<int>(n));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin>>mat[i][j];

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

	vector<ll> ans(n);
	for(int step = 0; step < n; step++) {
		ll cur = 0;

		for(int i = 0; i < n; i++) {
			bool any = false;
			for(int j = 0; j < n; j++)
				if(mat[i][j]) any = true;
			if(!any) continue;

			vector<ll> dist(n, oo);
			dist[i] = 0;
			typedef pair<ll, int> Node;
			priority_queue<Node, vector<Node>, greater<Node>> pq;
			pq.emplace(0, i);
			while(!pq.empty()) {
				int u = pq.top().second;
				ll d = pq.top().first;
				pq.pop();
				if(d > dist[u]) continue;
				for(int v = 0; v < n; v++) {
					if(mat[u][v] == 0) continue;
					//debug() << nav(e);
					int w = mat[u][v];
					if(d + w < dist[v]) {
						pq.emplace(d+w, v);
						dist[v] = d + w;
					}
				}
			}

			debug() << nav(i) << nav(dist);
			for(ll x : dist)
				if(x < oo)
					cur += x;
		}

		ans[step] = cur;

		int x = del[step];
		debug() << nav(x);
		for(int i = 0; i < n; i++)
			mat[i][x] = mat[x][i] = 0;
	}

	for(int i = 0; i < n; i++)
		cout << ans[i] << " \n"[i==n-1];

	return true;
}

int main(int argc, char* argv[]) {
#ifdef ONPC
	freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define nav(...) "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
#define oo INT_MAX/2
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
template<class T> struct rge { T b, e; };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
	~debug() {cerr<<endl;}
	template<class T> debug& operator<<(T x) {cerr<<boolalpha<<x; return *this;}
	template<class B, class C> debug& operator<< (pair<B, C> x){return *this<<"("<<x.first<<", "<<x.second<<")";}
	template<class T> debug& operator<<(rge<T> x) {
		*this<<"[";for(auto it=x.b;it!=x.e;it++){*this<<", "+2*(it==x.b)<<*it;}return *this<<"]";}
	template<class T> debug& operator<<(vector<T> x){ return *this<<range(all(x));}
#else
	template<class T> debug& operator <<(const T&) {return *this;}
#endif
};

// after removing a vertex, the graph is still complete!
// work backwards?

bool solve(void) {

	int n;
	cin>>n;
	vector<vector<int>> mat(n, vector<int>(n));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin>>mat[i][j];

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

	vector<ll> ans(n);

	for(int x = n - 1; x >= 0; x--) {
		int k = del[x];

		typedef pair<ll, int> Node;
		priority_queue<Node, vector<Node>, greater<Node>> pq;
		vector<ll> dist(n, oo);
		dist[k] = 0;
		pq.emplace(0, k);
		while(!pq.empty()) {
			ll d = pq.top().first;
			int u = pq.top().second;
			pq.pop();
			if(d > dist[u]) continue;
			for(int i = x; i < n; i++) {
				int v = del[i];
				int w = mat[u][v];
				if(d + w < dist[v]) {
					dist[v] = d + w;
					pq.emplace(d+w, v);
				}
			}
		}

		for(int i = x; i < n; i++) {
			int v = del[i];
			mat[k][v] = dist[v];
		}

		dist.assign(n, oo);
		dist[k] = 0;
		pq.emplace(0, k);
		while(!pq.empty()) {
			ll d = pq.top().first;
			int u = pq.top().second;
			pq.pop();
			if(d > dist[u]) continue;
			for(int i = x; i < n; i++) {
				int v = del[i];
				int w = mat[v][u];
				if(d + w < dist[v]) {
					dist[v] = d + w;
					pq.emplace(d+w, v);
				}
			}
		}

		for(int i = x; i < n; i++) {
			int v = del[i];
			mat[v][k] = dist[v];
		}

		for(int y = x+1; y < n; y++)
			for(int z = x+1; z < n; z++) {
				int i = del[y], j = del[z];
				mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
			}

		for(int i = x; i < n; i++) {
			for(int j = x; j < n; j++) {
				int a = del[i], b = del[j];
				ans[x] += mat[a][b];
			}
		}

	}

	for(int i = 0; i < n; i++)
		cout << ans[i] << " \n"[i==n-1];

	return true;
}

int main(int argc, char* argv[]) {
#ifdef ONPC
	freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define nav(...) "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
#define oo INT_MAX/2
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
template<class T> struct rge { T b, e; };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
	~debug() {cerr<<endl;}
	template<class T> debug& operator<<(T x) {cerr<<boolalpha<<x; return *this;}
	template<class B, class C> debug& operator<< (pair<B, C> x){return *this<<"("<<x.first<<", "<<x.second<<")";}
	template<class T> debug& operator<<(rge<T> x) {
		*this<<"[";for(auto it=x.b;it!=x.e;it++){*this<<", "+2*(it==x.b)<<*it;}return *this<<"]";}
	template<class T> debug& operator<<(vector<T> x){ return *this<<range(all(x));}
#else
	template<class T> debug& operator <<(const T&) {return *this;}
#endif
};

// after removing a vertex, the graph is still complete!
// work backwards?

bool solve(void) {

	int n;
	cin>>n;
	vector<vector<int>> dist(n, vector<int>(n));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin>>dist[i][j];

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

	vector<ll> ans(n);

	for(int x = n - 1; x >= 0; x--) {
		//int k = del[x];

		for(int a = x; a < n; a++)
			for(int y = x; y < n; y++)
				for(int z = x; z < n; z++) {
					int i = del[y], j = del[z], k = del[a];
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}

		for(int i = x; i < n; i++) {
			for(int j = x; j < n; j++) {
				int a = del[i], b = del[j];
				ans[x] += dist[a][b];
			}
		}

	}

	for(int i = 0; i < n; i++)
		cout << ans[i] << " \n"[i==n-1];

	return true;
}

int main(int argc, char* argv[]) {
#ifdef ONPC
	freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define nav(...) "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
#define oo INT_MAX/2
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
template<class T> struct rge { T b, e; };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
	~debug() {cerr<<endl;}
	template<class T> debug& operator<<(T x) {cerr<<boolalpha<<x; return *this;}
	template<class B, class C> debug& operator<< (pair<B, C> x){return *this<<"("<<x.first<<", "<<x.second<<")";}
	template<class T> debug& operator<<(rge<T> x) {
		*this<<"[";for(auto it=x.b;it!=x.e;it++){*this<<", "+2*(it==x.b)<<*it;}return *this<<"]";}
	template<class T> debug& operator<<(vector<T> x){ return *this<<range(all(x));}
#else
	template<class T> debug& operator <<(const T&) {return *this;}
#endif
};

// after removing a vertex, the graph is still complete!
// work backwards?

bool solve(void) {

	int n;
	cin>>n;
	vector<vector<int>> dist(n, vector<int>(n));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin>>dist[i][j];

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

	vector<ll> ans(n);

	for(int x = n - 1; x >= 0; x--) {
		int k = del[x];

		for(int y = x; y < n; y++)
			for(int z = x; z < n; z++) {
				int i = del[y], j = del[z];
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}

		for(int i = x; i < n; i++) {
			for(int j = x; j < n; j++) {
				int a = del[i], b = del[j];
				ans[x] += dist[a][b];
			}
		}

	}

	for(int i = 0; i < n; i++)
		cout << ans[i] << " \n"[i==n-1];

	return true;
}

int main(int argc, char* argv[]) {
#ifdef ONPC
	freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define nav(...) "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
#define oo INT_MAX/2
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
template<class T> struct rge { T b, e; };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
	~debug() {cerr<<endl;}
	template<class T> debug& operator<<(T x) {cerr<<boolalpha<<x; return *this;}
	template<class B, class C> debug& operator<< (pair<B, C> x){return *this<<"("<<x.first<<", "<<x.second<<")";}
	template<class T> debug& operator<<(rge<T> x) {
		*this<<"[";for(auto it=x.b;it!=x.e;it++){*this<<", "+2*(it==x.b)<<*it;}return *this<<"]";}
	template<class T> debug& operator<<(vector<T> x){ return *this<<range(all(x));}
#else
	template<class T> debug& operator <<(const T&) {return *this;}
#endif
};

bool solve(void) {

	int n;
	cin>>n;
	vector<vector<int>> mat(n, vector<int>(n));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin>>mat[i][j];

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

	vector<ll> ans(n);
	for(int step = 0; step < n; step++) {
		ll cur = 0;

		vector<vector<pii>> adj(n);
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				if(mat[i][j])
					adj[i].eb(j, mat[i][j]);

		for(int i = 0; i < n; i++) {
			bool any = false;
			for(int j = 0; j < n; j++)
				if(mat[i][j]) any = true;
			if(!any) continue;

			vector<ll> dist(n, oo);
			dist[i] = 0;
			typedef pair<ll, int> Node;
			priority_queue<Node, vector<Node>, greater<Node>> pq;
			pq.emplace(0, i);
			while(!pq.empty()) {
				int u = pq.top().second;
				ll d = pq.top().first;
				pq.pop();
				if(d > dist[u]) continue;
				for(pii e : adj[u]) {
					//debug() << nav(e);
					int v = e.first, w = e.second;
					if(d + w < dist[v]) {
						pq.emplace(d+w, v);
						dist[v] = d + w;
					}
				}
			}

			debug() << nav(i) << nav(dist);
			for(ll x : dist)
				if(x < oo)
					cur += x;
		}

		ans[step] = cur;

		int x = del[step];
		debug() << nav(x);
		for(int i = 0; i < n; i++)
			mat[i][x] = mat[x][i] = 0;
	}

	for(int i = 0; i < n; i++)
		cout << ans[i] << " \n"[i==n-1];

	return true;
}

int main(int argc, char* argv[]) {
#ifdef ONPC
	freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    long long g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    long long ans;
    long long dis[n+1][n+1];
    long long inf= 1e18;
    int x;
    for (int m=0; m<n; m++){
        cin>>x;
        for (int i=1 ; i<=n ; i++){
            for (int j=1 ; j<=n ;j++){
                dis[i][j]=g[i][j];
            }
        }
        for (int k=1 ; k<=n; k++)
            for (int i=1; i<=n ; i++)
                for (int j=1 ;j<=n ; j++)
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
        ans=0;
        for (int i=1; i<=n ; i++)
            for (int j=1 ; j<=n ; j++){
            	if (dis[i][j]<inf) ans+=dis[i][j];
                dis[i][j]=inf;
            }

        for (int j=1 ; j<=n; j++){
            g[j][x]=g[x][j]=inf;
        }
        cout<<ans<<' ';
    }
    return 0;
}

/*check for overflows
check for the conditions in if.. (i<n or i<=n)
use fast input/output
keep it simple
keep practicing*/


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    int g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    int dis[n+1][n+1];
    int inf= 1e9;
    for (int i=1 ; i<=n; i++){
        for (int j=1; j<=n ; j++){
            dis[i][j]=inf;
        }
    }
    int x[n];
    for (int i=0 ; i<n ;i++) cin>>x[i];
    long long ans[n];
    bool add[n+1];
    for (int i=1; i<=n ; i++) add[i]=false;
    for (int m=n-1;m>=0 ; m--){
        int k =x[m];
        ans[m]=0;
        add[k]=1;
        for (int i=1; i<=n ; i++){
            if (add[i]){
                dis[i][k]=g[i][k];
                dis[k][i]=g[k][i];
            }
        }
        for (int i=1 ; i<=n ; i++){
            for (int j=1 ; j<=n ; j++){
                dis[k][j]=min(dis[k][j],dis[k][i]+dis[i][j]);
            }
        }
        for (int i=1; i<=n ; i++){
            for (int j=1; j<=n ; j++){
                dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
            }
        }
        for (int i=1 ; i<=n ;i++){
            for (int j=1 ; j<=n ; j++){
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
        for (int i=1 ; i<=n; i++){
            for (int j=1 ; j<=n ;j++){
                if (dis[i][j]<inf ) ans[m]+=dis[i][j];
            }
        }
    }
    for (int i=0; i<n; i++) cout<<ans[i]<<' ';
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    long long g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    long long ans;
    long long dis[n+1][n+1][2];
    for (int i=1 ; i<=n ; i++){
        for (int j=1 ; j<=n; j++){
            dis[i][j][0]=g[i][j];
            dis[i][j][1]=0;
        }
    }
    long long inf= 1e18;
    int x;
    for (int m=0; m<n; m++){
        cin>>x;
        for (int k=1 ; k<=n; k++)
            for (int i=1; i<=n ; i++)
                for (int j=1 ;j<=n ; j++){
                    if (dis[i][j][1]<m) {
                        dis[i][j][0]=g[i][j];
                        dis[i][j][1]=m;
                    }
                    if (dis[i][k][1]<m) {
                        dis[i][k][0]=g[i][j];
                        dis[i][k][1]=m;
                    }
                    if (dis[k][j][1]<m) {
                        dis[k][j][0]=g[i][j];
                        dis[k][j][1]=m;
                    }
                    dis[i][j][0]=min(dis[i][j][0],dis[i][k][0]+dis[k][j][0]);
                }

        ans=0;
        for (int i=1; i<=n ; i++)
            for (int j=1 ; j<=n ; j++)
            	if (dis[i][j][0]<inf) ans+=dis[i][j][0];

        for (int j=1 ; j<=n; j++){
            g[j][x]=g[x][j]=inf;
        }
        cout<<ans<<' ';
    }
    return 0;
}

/*check for overflows
check for the conditions in if.. (i<n or i<=n)
use fast input/output
keep it simple
keep practicing*/


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    int g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    int dis[n+1][n+1];
    int inf= 1e9;
    for (int i=1 ; i<=n; i++){
        for (int j=1; j<=n ; j++){
            dis[i][j]=inf;
        }
    }
    int x[n];
    for (int i=0 ; i<n ;i++) cin>>x[i];
    int ans[n];
    bool add[n+1];
    for (int i=1; i<=n ; i++) add[i]=false;
    for (int m=n-1;m>=0 ; m--){
        int k =x[m];
        ans[m]=0;
        add[k]=1;
        for (int i=1; i<=n ; i++){
            if (add[i]){
                dis[i][k]=g[i][k];
                dis[k][i]=g[k][i];
            }
        }
        for (int i=1 ; i<=n ; i++){
            for (int j=1 ; j<=n ; j++){
                dis[k][j]=min(dis[k][j],dis[k][i]+dis[i][j]);
            }
        }
        for (int i=1; i<=n ; i++){
            for (int j=1; j<=n ; j++){
                dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
            }
        }
        for (int i=1 ; i<=n ;i++){
            for (int j=1 ; j<=n ; j++){
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
        for (int i=1 ; i<=n; i++){
            for (int j=1 ; j<=n ;j++){
                if (dis[i][j]<inf ) ans[m]+=dis[i][j];
            }
        }
    }
    for (int i=0; i<n; i++) cout<<ans[i]<<' ';
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    int g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    int ans;
    int dis[n+1][n+1];
    int inf= 1e9;
    int x;
    for (int m=0; m<n; m++){
        cin>>x;
        for (int i=1 ; i<=n ; i++){
            for (int j=1 ; j<=n ;j++){
                dis[i][j]=g[i][j];
            }
        }
        for (int k=1 ; k<=n; k++)
            for (int i=1; i<=n ; i++)
                for (int j=1 ;j<=n ; j++)
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
        ans=0;
        for (int i=1; i<=n ; i++)
            for (int j=1 ; j<=n ; j++){
            	if (dis[i][j]<inf) ans+=dis[i][j];
                dis[i][j]=inf;
            }

        for (int j=1 ; j<=n; j++){
            g[j][x]=g[x][j]=inf;
        }
        cout<<ans<<' ';
    }
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    int g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    int dis[n+1][n+1];
    int inf= 1e9;
    for (int i=1 ; i<=n; i++){
        for (int j=1; j<=n ; j++){
            dis[i][j]=inf;
        }
    }
    int x[n];
    for (int i=0 ; i<n ;i++) cin>>x[i];
    int ans[n];
    bool add[n+1];
    for (int i=1; i<=n ; i++) add[i]=false;
    for (int m=n-1;m>=0 ; m--){
        int k =x[m];
        ans[m]=0;
        add[k]=1;
        for (int i=1; i<=n ; i++){
            if (add[i]){
                dis[i][k]=g[i][k];
                dis[k][i]=g[k][i];
            }
        }
        for (int k=1 ; k<=n; k++){
            for (int i=1 ; i<=n ;i++){
                for (int j=1 ; j<=n ; j++){
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
                }
            }
        }
        for (int i=1 ; i<=n; i++){
            for (int j=1 ; j<=n ;j++){
                if (dis[i][j]<inf ) ans[m]+=dis[i][j];
            }
        }
    }
    for (int i=0; i<n; i++) cout<<ans[i]<<' ';
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n; cin>>n;
    int g[n+1][n+1];
    for (int i=1; i<=n ; i++)
        for (int j=1 ; j<=n; j++)
            cin>>g[i][j];
    int dis[n+1][n+1];
    int inf= 1e9;
    for (int i=1 ; i<=n; i++){
        for (int j=1; j<=n ; j++){
            dis[i][j]=inf;
        }
    }
    int x[n];
    for (int i=0 ; i<n ;i++) cin>>x[i];
    int ans[n];
    bool add[n+1];
    for (int i=1; i<=n ; i++) add[i]=false;
    for (int m=n-1;m>=0 ; m--){
        int k =x[m];
        ans[m]=0;
        add[k]=1;
        for (int i=1; i<=n ; i++){
            if (add[i]){
                dis[i][k]=g[i][k];
                dis[k][i]=g[k][i];
            }
        }
        for (int i=1 ; i<=n ;i++){
            for (int j=1 ; j<=n ; j++){
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
        for (int i=1 ; i<=n; i++){
            for (int j=1 ; j<=n ;j++){
                if (dis[i][j]<inf ) ans[m]+=dis[i][j];
            }
        }
    }
    for (int i=0; i<n; i++) cout<<ans[i]<<' ';
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<vector<long int>> a(n);
	vector<vector<long int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<long int>(n);
		d[i] = vector<long int>(n);

		for (int j = 0; j < n; j++)
		{
			long int temp;
			cin >> temp;
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<bool> deleted(n,0);

	for (int l = 0; l < n; l++)
	{
		d = a;
		for (int k = 0; k < n; k++)
		{
			if (!deleted[k])
			{
				for (int i = 0; i < n; i++)
				{
					if (!deleted[i])
					{
						for (int j = 0; j < n; j++)
						{
							if (!deleted[j])
							{
								if(d[i][j] > d[i][k] + d[k][j])
								{
									d[i][j] = d[i][k] + d[k][j];
								}
							}
						}
					}
				}
			}
		}

		long int sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (!deleted[i] && !deleted[j]) sum += d[i][j];
			}
		}

		cout << sum << " ";

		deleted[x[l]] = 1;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<vector<long int>> a(n);
	vector<vector<long int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<long int>(n);
		d[i] = vector<long int>(n);

		for (int j = 0; j < n; j++)
		{
			long int temp;
			cin >> temp;
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<bool> deleted(n,0);

	for (int l = 0; l < n; l++)
	{
		d = a;
		for (int k = 0; k < n; k++)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					if (!deleted[k] && !deleted[i] && !deleted[j])
					{
						d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
					}
				}
			}
		}

		long int sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (!deleted[i] && !deleted[j]) sum += d[i][j];
			}
		}

		cout << sum << " ";

		deleted[x[l]] = 1;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<unsigned int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		/*for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}*/

		// x[l]. csucson megyunk vegig
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[l]][x[i]] = min(d[x[l]][x[i]],d[x[l]][x[k]] + d[x[k]][x[i]]);
			}

			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[i]][x[l]] = min(d[x[i]][x[l]],d[x[i]][x[k]] + d[x[k]][x[l]]);
			}
		}

		//az osszesen ugy, hogy az x[l]-et is hasznaljuk
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		unsigned int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%d ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			cin >> temp;
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<bool> deleted(n,0);
	int first = 0;

	for (int l = 0; l < n; l++)
	{
		for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					d[i][j] = min(a[i][j],a[i][first] + a[first][j]);
				}
			}

		for (int k = first + 1; k < n; k++)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					if (!deleted[k] && !deleted[i] && !deleted[j])
					{
						d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
					}
				}
			}
		}

		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (!deleted[i] && !deleted[j]) sum += d[i][j];
			}
		}

		cout << sum << " ";

		deleted[x[l]] = 1;
		if (first == x[l])
		{
			for (int i = first; i < n; i++)
			{
				if (!deleted[i])
				{
					first = i;
					break;
				}
			}
		}
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<unsigned int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		/*for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}*/

		// x[l]. csucson megyunk vegig
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[l]][x[i]] = min(d[x[l]][x[i]],d[x[l]][x[k]] + d[x[k]][x[i]]);
			}

			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[i]][x[l]] = min(d[x[i]][x[l]],d[x[i]][x[k]] + d[x[k]][x[l]]);
			}
		}

		//az osszesen ugy, hogy az x[l]-et is hasznaljuk
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		unsigned int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%d ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		/*for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}*/

		// x[l]. csucson megyunk vegig
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[l]][x[i]] = min(d[x[l]][x[i]],d[x[l]][x[k]] + d[x[k]][x[i]]);
			}

			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[i]][x[l]] = min(d[x[i]][x[l]],d[x[i]][x[k]] + d[x[k]][x[l]]);
			}
		}

		//az osszesen ugy, hogy az x[l]-et is hasznaljuk
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%d ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%d ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}

		int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%d ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<vector<long int>> a(n);
	vector<vector<long int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<long int>(n);
		d[i] = vector<long int>(n);

		for (int j = 0; j < n; j++)
		{
			long int temp;
			cin >> temp;
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<bool> deleted(n,0);

	for (int l = 0; l < n; l++)
	{
		d = a;
		for (int k = 0; k < n; k++)
		{
			if (!deleted[k])
			{
				for (int i = 0; i < n; i++)
				{
					if (!deleted[i])
					{
						for (int j = 0; j < n; j++)
						{
							if (!deleted[j])
							{
								d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
							}
						}
					}
				}
			}
		}

		long int sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (!deleted[i] && !deleted[j]) sum += d[i][j];
			}
		}

		cout << sum << " ";

		deleted[x[l]] = 1;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<unsigned long int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		/*for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}*/

		// x[l]. csucson megyunk vegig
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[l]][x[i]] = min(d[x[l]][x[i]],d[x[l]][x[k]] + d[x[k]][x[i]]);
			}

			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[i]][x[l]] = min(d[x[i]][x[l]],d[x[i]][x[k]] + d[x[k]][x[l]]);
			}
		}

		//az osszesen ugy, hogy az x[l]-et is hasznaljuk
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		unsigned long int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%lu ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer

	for (int l = 0; l < n; l++)
	{
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = a[x[i]][x[j]];
			}
		}

		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					d[i][j] = min(d[i][j],d[i][x[k]] + d[x[k]][j]);
				}
			}
		}

		int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				sum += d[x[i]][x[j]];
			}
		}

		printf("%d ",sum);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}

		int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%d ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer

	for (int l = 0; l < n; l++)
	{
		d = a;

		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					d[i][j] = min(d[i][j],d[i][x[k]] + d[x[k]][j]);
				}
			}
		}

		int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				sum += d[x[i]][x[j]];
			}
		}

		printf("%d ",sum);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			scanf("%d",&temp);
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<bool> deleted(n,0);
	int first = 0;

	for (int l = 0; l < n; l++)
	{
		for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					d[i][j] = min(a[i][j],a[i][first] + a[first][j]);
				}
			}

		for (int k = first + 1; k < n; k++)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					if (!deleted[k] && !deleted[i] && !deleted[j])
					{
						d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
					}
				}
			}
		}

		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (!deleted[i] && !deleted[j]) sum += d[i][j];
			}
		}

		printf("%d ",sum);

		deleted[x[l]] = 1;
		if (first == x[l])
		{
			for (int i = first; i < n; i++)
			{
				if (!deleted[i])
				{
					first = i;
					break;
				}
			}
		}
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	// vector<vector<int>> a(n);
	vector<vector<unsigned long int>> d(n);

	for (int i = 0; i < n; i++)
	{
		// a[i] = vector<int>(n);
		d[i] = vector<unsigned long int>(n);

		for (int j = 0; j < n; j++)
		{
			unsigned long int temp;
			scanf("%lu",&temp);
			// a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<unsigned long int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		/*for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}*/

		// x[l]. csucson megyunk vegig
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[l]][x[i]] = min(d[x[l]][x[i]],d[x[l]][x[k]] + d[x[k]][x[i]]);
			}

			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[i]][x[l]] = min(d[x[i]][x[l]],d[x[i]][x[k]] + d[x[k]][x[l]]);
			}
		}

		//az osszesen ugy, hogy az x[l]-et is hasznaljuk
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		unsigned long int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%lu ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	// vector<vector<int>> a(n);
	vector<vector<unsigned long long int>> d(n);

	for (int i = 0; i < n; i++)
	{
		// a[i] = vector<int>(n);
		d[i] = vector<unsigned long long int>(n);

		for (int j = 0; j < n; j++)
		{
			unsigned long long int temp;
			scanf("%llu",&temp);
			// a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d",&x[i]);
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<unsigned long long int> res(n);

	for (int l = n - 1; l >= 0; l--)
	{
		/*for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				for (int j = n - 1; j > l - 1; j--)
				{
					d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[k]] + d[x[k]][x[j]]);
				}
			}
		}*/

		// x[l]. csucson megyunk vegig
		for (int k = n - 1; k > l - 1; k--)
		{
			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[l]][x[i]] = min(d[x[l]][x[i]],d[x[l]][x[k]] + d[x[k]][x[i]]);
			}

			for (int i = n - 1; i > l - 1; i--)
			{
				d[x[i]][x[l]] = min(d[x[i]][x[l]],d[x[i]][x[k]] + d[x[k]][x[l]]);
			}
		}

		//az osszesen ugy, hogy az x[l]-et is hasznaljuk
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j--)
			{
				d[x[i]][x[j]] = min(d[x[i]][x[j]],d[x[i]][x[l]] + d[x[l]][x[j]]);
			}
		}

		unsigned long long int sum = 0;
		for (int i = n - 1; i > l - 1; i--)
		{
			for (int j = n - 1; j > l - 1; j --)
			{
				sum += d[x[i]][x[j]];
			}
		}

		res[l] = sum;
	}

	for (int i = 0; i < n; i++)
	{
		printf("%llu ",res[i]);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<vector<int>> a(n);
	vector<vector<int>> d(n);

	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(n);
		d[i] = vector<int>(n);

		for (int j = 0; j < n; j++)
		{
			int temp;
			cin >> temp;
			a[i][j] = temp;
			d[i][j] = temp;
		}
	}

	vector<int> x(n);

	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
		x[i]--;
	}

	//floyd-warshall n-szer
	vector<bool> deleted(n,0);

	for (int l = 0; l < n; l++)
	{
		d = a;
		for (int k = 0; k < n; k++)
		{
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					if (!deleted[k] && !deleted[i] && !deleted[j])
					{
						d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
					}
				}
			}
		}

		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (!deleted[i] && !deleted[j]) sum += d[i][j];
			}
		}

		cout << sum << " ";

		deleted[x[l]] = 1;
	}

	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define MP make_pair
#define PB push_back
#define ll long long
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
#define mod 1000000007
int n;
int a[501][501];
int x[501];
vector <int> an;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	f(i,1,n+1)
		f(j,1,n+1)
			cin>>a[i][j];
	for(int i=n;i>0;i--)
		cin>>x[i];
	f(i,1,n+1)
	{
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		int ans=0;
		f(j,1,i+1)
			f(k,1,i+1)
				ans+=a[x[j]][x[k]];
		an.push_back(ans);
	}
	reverse(an.begin(), an.end());
	for(int i: an)
		cout<<i<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define MP make_pair
#define PB push_back
#define ll long long
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
#define mod 1000000007
int n;
int a[501][501];
int x[501];
vector <int> an;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	f(i,1,n+1)
		f(j,1,n+1)
			cin>>a[i][j];
	for(int i=n;i>0;i--)
		cin>>x[i];
	f(i,1,n+1)
	{
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				// a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				// a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		int ans=0;
		f(j,1,i+1)
			f(k,1,i+1)
				ans+=a[x[j]][x[k]];
		an.push_back(ans);
	}
	reverse(an.begin(), an.end());
	for(int i: an)
		cout<<i<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define MP make_pair
#define PB push_back
#define ll long long
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
#define mod 1000000007
int n;
int a[501][501];
int x[501];
vector <int> an;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	f(i,1,n+1)
		f(j,1,n+1)
			cin>>a[i][j];
	for(int i=n;i>0;i--)
		cin>>x[i];
	f(i,1,n+1)
	{
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				// a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				// a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		f(j,1,i)
		{
			f(k,1,i)
			{
				// a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		int ans=0;
		f(j,1,i+1)
			f(k,1,i+1)
				ans+=a[x[j]][x[k]];
		an.push_back(ans);
	}
	reverse(an.begin(), an.end());
	for(int i: an)
		cout<<i<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define MP make_pair
#define PB push_back
#define ll long long
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
#define mod 1000000007
int n;
int a[501][501];
int x[501];
vector <int> an;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	f(i,1,n+1)
		f(j,1,n+1)
			cin>>a[i][j];
	for(int i=n;i>0;i--)
		cin>>x[i];
	f(i,1,n+1)
	{
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
			}
		}
		int ans=0;
		f(j,1,i+1)
			f(k,1,i+1)
				ans+=a[x[j]][x[k]];
		an.push_back(ans);
	}
	reverse(an.begin(), an.end());
	for(int i: an)
		cout<<i<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define MP make_pair
#define PB push_back
#define ll long long
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
#define mod 1000000007
int n;
int a[501][501];
int x[501];
vector <int> an;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	f(i,1,n+1)
		f(j,1,n+1)
			cin>>a[i][j];
	for(int i=n;i>0;i--)
		cin>>x[i];
	f(i,1,n+1)
	{
		f(j,1,i)
		{
			f(k,1,i)
			{
				// a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				// a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				// a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		int ans=0;
		f(j,1,i+1)
			f(k,1,i+1)
				ans+=a[x[j]][x[k]];
		an.push_back(ans);
	}
	reverse(an.begin(), an.end());
	for(int i: an)
		cout<<i<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define MP make_pair
#define PB push_back
#define ll long long
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
#define mod 1000000007
int n;
int a[501][501];
int x[501];
vector <int> an;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	f(i,1,n+1)
		f(j,1,n+1)
			cin>>a[i][j];
	for(int i=n;i>0;i--)
		cin>>x[i];
	f(i,1,n+1)
	{
		f(j,1,i)
		{
			f(k,1,i)
			{
				a[x[j]][x[k]]=min({a[x[j]][x[k]], a[x[j]][x[i]]+a[x[i]][x[k]] });
				a[x[j]][x[i]]=min({a[x[j]][x[i]], a[x[j]][x[k]]+a[x[k]][x[i]] });
				a[x[i]][x[j]]=min({a[x[i]][x[j]], a[x[i]][x[k]]+a[x[k]][x[j]] });
			}
		}
		int ans=0;
		f(j,1,i+1)
			f(k,1,i+1)
				ans+=a[x[j]][x[k]];
		an.push_back(ans);
	}
	reverse(an.begin(), an.end());
	for(int i: an)
		cout<<i<<" ";
	cout<<endl;
	return 0;
}


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> adj[i][j];
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    reverse(order.begin() , order.end());
    vector<int> ans(1 , 0LL);
    vector<vector<int>> d(n , vector<int>(n , INF));
    vector<int> alive(n);
    alive[order[0]] = 1;
    for(int i = 0; i < n; i++) d[i][i] = 0;
    for(int i = 1; i < n; i++){
        int csum = 0;
        alive[order[i]] = 1;
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                if(u == v || alive[u] == 0 || alive[v] == 0) continue;
                d[u][v] = min(d[u][v] , adj[u][order[i]] + adj[order[i]][v]);
            }
        }
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                if(alive[u] == 0 || alive[v] == 0) continue;
                csum += d[u][v];
            }
        }
        ans.push_back(csum);
    }
    reverse(ans.begin(), ans.end());
    for(int it : ans){
        cout << it << " ";
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int weight; cin >> weight;
            adj[i][j] = weight;
            g[i].insert({j , weight});
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    for(int i = 0; i < n; i++){
        int ans = 0;
        for(int s = 0; s < n; s++){
            if(dead[s]) continue;
            vector<int> d(n , INF);
            set<pair<int , int>> se;
            se.insert({0 , s});
            d[s] = 0;
            while(!se.empty()){
                auto from = *se.begin();
                se.erase(se.begin());
                for(auto v : g[from.second]){
                    if(d[from.second] + v.second < d[v.first]){
                        se.erase({d[v.first] , v.first});
                        d[v.first] = d[from.second] + v.second;
                        se.insert({d[v.first] , v.first});
                    }
                }
            }
            for(int t = 0; t < n; t++){
                if(d[t] == INF) continue;
                ans += d[t];
            }
        }
        cout << ans << " ";
        int vertex = order[i];
        dead[vertex] = 1;
        for(int j = 0; j < n; j++){
            if(j == vertex) continue;
            g[j].erase({vertex , adj[j][vertex]});
        }
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// #pragma warning(disable:4996)
// #pragma comment(linker, "/STACK:336777216")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int weight; cin >> weight;
            adj[i][j] = weight;
            g[i].insert({j , weight});
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    for(int i = 0; i < n; i++){
        int ans = 0;
        for(int s = 0; s < n; s++){
            if(dead[s]) continue;
            vector<int> d(n , INF);
            set<pair<int , int>> se;
            se.insert({0 , s});
            d[s] = 0;
            while(!se.empty()){
                auto from = *se.begin();
                se.erase(se.begin());
                for(auto v : g[from.second]){
                    if(d[from.second] + v.second < d[v.first]){
                        se.erase({d[v.first] , v.first});
                        d[v.first] = d[from.second] + v.second;
                        se.insert({d[v.first] , v.first});
                    }
                }
            }
            for(int t = 0; t < n; t++){
                if(d[t] == INF) continue;
                ans += d[t];
            }
        }
        cout << ans << " ";
        int vertex = order[i];
        dead[vertex] = 1;
        for(int j = 0; j < n; j++){
            if(j == vertex) continue;
            g[j].erase({vertex , adj[j][vertex]});
        }
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> adj[i][j];
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    reverse(order.begin() , order.end());
    vector<int> ans;
    vector<vector<int>> d(n , vector<int>(n , INF));
    vector<int> alive(n);
    for(int i = 0; i < n; i++) d[i][i] = 0;
    for(int i = 0; i < n; i++){
        int csum = 0;
        alive[order[i]] = 1;
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                d[u][v] = min(d[u][v] , adj[u][order[i]] + adj[order[i]][v]);
            }
        }
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                if(alive[u] == 0 || alive[v] == 0) continue;
                csum += d[u][v];
            }
        }
        ans.push_back(csum);
    }
    reverse(ans.begin(), ans.end());
    for(int it : ans){
        cout << it << " ";
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> adj[i][j];
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    reverse(order.begin() , order.end());
    vector<int> ans;
    vector<vector<int>> d(n , vector<int>(n , INF));
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) d[i][j] = adj[i][j];
    vector<int> alive(n);
    for(int i = 0; i < n; i++) d[i][i] = 0;
    for(int i = 0; i < n; i++){
        int csum = 0;
        alive[order[i]] = 1;
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                d[u][v] = min(d[u][v] , d[u][order[i]] + d[order[i]][v]);
            }
        }
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                if(alive[u] == 0 || alive[v] == 0) continue;
                csum += d[u][v];
            }
        }
        ans.push_back(csum);
    }
    reverse(ans.begin(), ans.end());
    for(int it : ans){
        cout << it << " ";
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int weight; cin >> weight;
            adj[i][j] = weight;
            g[i].insert({j , weight});
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    for(int i = 0; i < n; i++){
        int ans = 0;
        for(int s = 0; s < n; s++){
            if(dead[s]) continue;
            vector<int> d(n , INF);
            priority_queue<pair<int , int> , vector<pair<int , int>> , greater<pair<int , int>>> se;
            se.push({0 , s});
            d[s] = 0;
            while(!se.empty()){
                auto from = se.top();
                se.pop();
                if(from.first != d[from.second]) continue;
                for(auto v : g[from.second]){
                    if(d[from.second] + v.second < d[v.first]){
                        d[v.first] = d[from.second] + v.second;
                        se.push({d[v.first] , v.first});
                    }
                }
            }
            for(int t = 0; t < n; t++){
                if(d[t] == INF) continue;
                ans += d[t];
            }
        }
        cout << ans << " ";
        int vertex = order[i];
        dead[vertex] = 1;
        for(int j = 0; j < n; j++){
            if(j == vertex) continue;
            g[j].erase({vertex , adj[j][vertex]});
        }
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


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

#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(x, y) memset(x, y, sizeof x);
#define y1 asdgh
#define int long long
#define double long double
// #define endl '\n'
#define PI 3.14159265359
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*-----------------------------------------------------------------------*/

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

/*-----------------------------------------------------------------------*/
const int maxn = (int) 1e6 + 6;
const double EPS = 1e-9;
const int INF = (int) 1e18 + 18;
const int mod = (int) 1e9 + 7;
int n , dead[maxn] , adj[510][510];
set<pair<int , int>> g[maxn];

signed main() {

    fio;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> adj[i][j];
        }
    }
    vector<int> order(n);
    for(int i = 0; i < n; i++){
        cin >> order[i]; order[i]--;
    }
    reverse(order.begin() , order.end());
    vector<int> ans;
    vector<int> alive(n);
    for(int i = 0; i < n; i++){
        int csum = 0;
        alive[order[i]] = 1;
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                adj[u][v] = min(adj[u][v] , adj[u][order[i]] + adj[order[i]][v]);
            }
        }
        for(int u = 0; u < n; u++){
            for(int v = 0; v < n; v++){
                if(alive[u] == 0 || alive[v] == 0) continue;
                csum += adj[u][v];
            }
        }
        ans.push_back(csum);
    }
    reverse(ans.begin(), ans.end());
    for(int it : ans){
        cout << it << " ";
    }
    cout << endl;

    return 0;
}

// IMPORTANT -:
// partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) a[0] . a[1] + a[0] ,......
// adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) - a[0] , a[1] - a[0] ,......
// check base cases n = 0 , n = 1
// max / min questions - check for binary search
// difference array might be interesting


#include<bits/stdc++.h>
using namespace std;
#define MOD 100000000
#define ll long long
#define SIZE 1000100
#define MX 1e6

int FW[510][510];
int ini[510][510];

void assign(int n)
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			FW[i][j] = ini[i][j];
		}
	}
}

void run(int n)
{
	for (int k = 1; k <= n; ++k)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int i = 1; i <= n; ++i)
			{
				if (FW[i][k] + FW[k][j] < FW[i][j])
					FW[i][j] = FW[i][k] + FW[k][j];
			}
		}
	}
}

void sumer(int n)
{
	int sum = 0;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			if (FW[i][j] < MX)
				sum += FW[i][j];
		}
	}
	cout << sum << " ";
}

void printer(int n)
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
			cout << FW[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}

int main()
{

	ios::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, a, b; scanf("%d", &n);

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			scanf("%d", &ini[i][j]);
		}
	}

	run(n);

	for (int i = 1; i <= n; ++i)
	{
		assign(n);
		run(n);
		sumer(n);
		scanf("%d", &b);
		for (int j = 1; j <= n; ++j)
		{
			ini[j][b] = 0;
			ini[b][j] = MX;
		}
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 100000000
#define ll long long
#define SIZE 1000100
#define MX 1e6

ll FW[510][510];
int rem[510];
ll ans[510];

int main()
{

	ios::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n; scanf("%d", &n);

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			scanf("%lld", &FW[i][j]);
		}
	}

	for (int i = 1; i <= n; ++i)
	{
		scanf("%d", &rem[i]);
	}

	for (int k = n; k > 0; --k)
	{
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= n; ++j)
			{
				FW[i][j] = min(FW[i][rem[k]] + FW[rem[k]][j], FW[i][j]);
			}
		}
		ll sum = 0;
		for (int i = k; i <= n; ++i)
		{
			for (int j = k; j <= n; ++j)
			{
				sum += FW[rem[i]][rem[j]];
			}
		}
		ans[k] = sum;
	}

	for (int i = 1; i <= n; ++i)
	{
		cout << ans[i] << " ";
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 100000000
#define ll long long
#define SIZE 1000100
#define MX 1e6

ll FW[510][510];
ll ini[510][510];
ll rem[510];
ll ans[510];
unordered_set<ll>done;

void addv(ll x, ll n)
{
	for (ll i = 1; i <= n; ++i)
	{
		if (done.count(i))
		{
			FW[x][i] = ini[x][i];
			FW[i][x] = ini[i][x];
		}
	}
	done.insert(x);
}

void printer(ll n)
{
	for (ll k = 1; k <= n; ++k)
	{
		for (ll j = 1; j <= n; ++j)
		{
			cout << FW[k][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void run(ll n)
{
	for (ll k = 1; k <= n; ++k)
	{
		for (ll j = 1; j <= n; ++j)
		{
			for (ll i = 1; i <= n; ++i)
			{
				if (FW[i][k] + FW[k][j] < FW[i][j])
					FW[i][j] = FW[i][k] + FW[k][j];
			}
		}
	}
}

ll sumer(ll n)
{
	ll sum = 0;
	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			if (FW[i][j] < MX)
				sum += FW[i][j];
		}
	}
	return sum;
}

int main()
{

	ios::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll n, a, b; scanf("%lld", &n);

	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			scanf("%lld", &ini[i][j]);
			FW[i][j] = MX;
			if (i == j)
				FW[i][j] = 0;
		}
	}

	for (ll i = 1; i <= n; ++i)
	{
		scanf("%lld", &rem[i]);
	}

	ll sum = 0;

	for (ll i = n; i > 0; i--)
	{
		addv(rem[i], n);
		run(n);
		ans[i] = sumer(n);
		// printer(n);
	}

	for (ll i = 1; i <= n; ++i)
		cout << ans[i] << " ";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 100000000
#define ll long long
#define SIZE 1000100
#define MX 1e6

ll FW[510][510];
ll ini[510][510];
ll rem[510];
ll ans[510];
unordered_set<ll>done;

void addv(ll x, ll n)
{
	for (ll i = 1; i <= n; ++i)
	{
		if (done.count(i))
		{
			FW[x][i] = ini[x][i];
			FW[i][x] = ini[i][x];
		}
	}
	done.insert(x);
}

void printer(ll n)
{
	for (ll k = 1; k <= n; ++k)
	{
		for (ll j = 1; j <= n; ++j)
		{
			cout << FW[k][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void run(ll x, ll n)
{
	for (ll j = 1; j <= n; ++j)
	{
		for (ll i = 1; i <= n; ++i)
		{
			if (FW[i][x] + FW[x][j] < FW[i][j])
				FW[i][j] = FW[i][x] + FW[x][j];
		}
	}
}

ll sumer(ll n)
{
	ll sum = 0;
	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			if (FW[i][j] < MX)
				sum += FW[i][j];
		}
	}
	return sum;
}

int main()
{

	ios::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll n, a, b; scanf("%lld", &n);

	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			scanf("%lld", &ini[i][j]);
			FW[i][j] = MX;
			if (i == j)
				FW[i][j] = 0;
		}
	}

	for (ll i = 1; i <= n; ++i)
	{
		scanf("%lld", &rem[i]);
	}

	ll sum = 0;

	for (ll i = n; i > 0; i--)
	{
		addv(rem[i], n);
		run(rem[i], n);
		ans[i] = sumer(n);
		// printer(n);
	}

	for (ll i = 1; i <= n; ++i)
		cout << ans[i] << " ";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 100000000
#define ll long long
#define SIZE 1000100
#define MX 1e6

ll FW[510][510];
ll ini[510][510];

void assign(ll n)
{
	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			FW[i][j] = ini[i][j];
		}
	}
}

void run(ll n)
{
	for (ll k = 1; k <= n; ++k)
	{
		for (ll j = 1; j <= n; ++j)
		{
			for (ll i = 1; i <= n; ++i)
			{
				if (FW[i][k] + FW[k][j] < FW[i][j])
					FW[i][j] = FW[i][k] + FW[k][j];
			}
		}
	}
}

void sumer(ll n)
{
	ll sum = 0;
	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			if (FW[i][j] < MX)
				sum += FW[i][j];
		}
	}
	cout << sum << " ";
}

void prller(ll n)
{
	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
			cout << FW[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}

int main()
{

	ios::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll n, a, b; scanf("%lld", &n);

	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= n; ++j)
		{
			scanf("%lld", &ini[i][j]);
		}
	}

	run(n);

	for (ll i = 1; i <= n; ++i)
	{
		assign(n);
		run(n);
		sumer(n);
		scanf("%lld", &b);
		for (ll j = 1; j <= n; ++j)
		{
			ini[j][b] = 0;
			ini[b][j] = MX;
		}
	}

	return 0;
}


#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];
            }
        }
        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 <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];
            }
        }
        active[order[n-1]] = true;
        vector <lli> ans(1,0);
        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] && fw[i][j] != INF)
                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] = INF;
            }
        }
        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++){
                if(!active[j])
                    continue;
                fw[order[i]][j] = min(fw[order[i]][j], md[order[i]][j]);
                fw[j][order[i]] = min(fw[j][order[i]], md[j][order[i]]);
            }
            for(lli j = 0; j<n; j++){
                for(lli k = 0; k<n; k++){
                    if(active[j])
                        fw[j][k] = min(fw[j][k], fw[j][order[i]] + fw[order[i]][k]);
                }
            }

            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<<" ";
    }
}