#include <iostream>
#include <climits>
using namespace std;
int a[100][100];
bool visited[100];
int dist[100];
void DijkstraPath(int n)
{
dist[0] = 0;
int i;
for (i = 1; i < n; i++)
dist[i] = INT_MAX;
for (; ;){
int min = INT_MAX, v = 0;
for (i = 0; i < n; i++)
if (!visited[i] && dist[i] < min)
min = dist[i], v = i;
if (min == INT_MAX) break;
visited[v] = true;
for (int j = 0; j < n; j++)
if (!visited[j] && a[v][j] < INT_MAX && dist[j] > dist[v] + a[v][j])
dist[j] = dist[v] + a[v][j];
}
}
int main()
{
int n, m;
cin >> n >> m;
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> a[i][j];
DijkstraPath(n);
cout << dist[m] << endl;
return 0;
}