using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static private int Compute(int[] a)
        {
            Dictionary<int, List<int>> d = new Dictionary<int, List<int>>();
            int minval = -1;
            for (int i = 0; i < a.Length; i++)
            {
                if (d.ContainsKey(a[i]))
                {
                    d[a[i]].Add(i);
                }
                else
                {
                    d.Add(a[i], new List<int>(){i});
                }
                minval = minval == -1 || minval > a[i] ? a[i] : minval;
            }
            int result = -1;
            int[] b = d[minval].ToArray();
            for (int i = 1; i < b.Length; i++)
            {
                if (result == -1 || b[i] - b[i - 1] < result)
                {
                    result = b[i] - b[i -1];
                }
            }
            return result;
        }

        static public int Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            var ss = s2.Split(' ');
            int[] a = new int[ss.Length];
            if (a.Length != int.Parse(s1))
            {
                throw new Exception();
            }
            for (int i = 0; i < ss.Length; i++)
            {
                a[i] = int.Parse(ss[i]);
            }
            int result = Compute(a);
            Console.WriteLine(result);
            return 0;
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static public int Main()
        {
            StreamReader sr = new StreamReader("standard.input");
            string s1 = sr.ReadLine();
            string s2 = sr.ReadLine();
            sr.Close();
            int result = 0;
            StreamWriter sw = new StreamWriter("standard.output");
            sw.WriteLine(result);
            sw.Close();
            return 0;
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static private int Compute(int[] a)
        {
            int result = 0;
            return result;
        }

        static public int Main()
        {
            StreamReader sr = new StreamReader("standard.input");
            string s1 = sr.ReadLine();
            string s2 = sr.ReadLine();
            sr.Close();
            var ss = s2.Split(' ');
            int[] a = new int[ss.Length];
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = int.Parse(ss[i]);
            }
            int result = Compute(a);
            StreamWriter sw = new StreamWriter("standard.output");
            sw.WriteLine(result);
            sw.Close();
            return 0;
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static private int Compute(int[] a)
        {
            Dictionary<int, List<int>> d = new Dictionary<int, List<int>>();
            for (int i = 0; i < a.Length; i++)
            {
                if (d.ContainsKey(a[i]))
                {
                    d[a[i]].Add(i);
                }
                else
                {
                    d.Add(a[i], new List<int>(){i});
                }
            }
            int result = -1;
            foreach (var key in d.Keys)
            {
                int[] b = d[key].ToArray();
                for (int i = 1; i < b.Length; i++)
                {
                    if (result == -1 || b[i] - b[i - 1] < result)
                    {
                        result = b[i] - b[i -1];
                    }
                }
            }
            return result;
        }

        static public int Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            var ss = s2.Split(' ');
            int[] a = new int[ss.Length];
            for (int i = 0; i < ss.Length; i++)
            {
                a[i] = int.Parse(ss[i]);
            }
            int result = Compute(a);
            Console.WriteLine(result);
            return 0;
        }

    }
}


using System;
using System.Linq;
class x
{
	static void Main()
	{
	    var count=0;
		var n = int.Parse(Console.ReadLine());
		var arr = Array.ConvertAll(System.Console.ReadLine().Split(), int.Parse);
		var min = arr.Min();
		var result = arr.Length;
		for(var i=Array.IndexOf(arr,min)+1;i<n;i++)
		{
		    if(arr[i] != min ) ++count;
		    else
		    {
		        ++count;
		        if(count<result) result = count;
		        count=0;
		    }
		}
		Console.Write(result);
	}
}


import java.lang.Integer.MAX_VALUE
import java.lang.Integer.min
import java.util.*

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    val N = nextInt()
    var minimum = MAX_VALUE
    var distance = 0
    var globalMininum = MAX_VALUE
    for(i in 1..N) {
        val x = nextInt()
        if(minimum > x) {
            minimum = x
            distance = 1
        } else if(minimum == x) {
            globalMininum = min(globalMininum, distance)
            distance = 1
        } else distance++
    }
    print(globalMininum)
}


import java.lang.Integer.MAX_VALUE
import java.lang.Integer.min
import java.util.*

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    val N = nextInt()
    var minimum = MAX_VALUE
    var distance = 0
    var globalMininum = MAX_VALUE
    for(i in 1..N) {
        val x = nextInt()
        if(minimum > x) {
            minimum = x
            distance = 1
            globalMininum = MAX_VALUE
        } else if(minimum == x) {
            globalMininum = min(globalMininum, distance)
            distance = 1
        } else distance++
    }
    print(globalMininum)
}


import java.lang.Integer.MAX_VALUE
import java.lang.Integer.min
import java.util.*

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    val N = nextInt()
    var minimum = MAX_VALUE
    var distance = 0
    var globalMininum = MAX_VALUE
    for(i in 1..N) {
        val x = nextInt()
        when {
            minimum > x -> {
                minimum = x
                distance = 1
                globalMininum = MAX_VALUE
            }
            minimum == x -> {
                globalMininum = min(globalMininum, distance)
                distance = 1
            }
            else -> distance++
        }
    }
    print(globalMininum)
}


fun main(args: Array<String>) {
	readLine()  // skip
	var min1: Int = 1000 * 1000 * 1000 + 1
	var min2: Int = 100000 + 1
	var last_min_pos = 0

	var pos = 0
	for(element in (readLine()!!.split(' ').map{it.toInt()})) {
		if(element < min1) {
			min1 = element
			min2 = 100000 + 1
			last_min_pos = pos
		} else if(element == min1) {
			if(pos - last_min_pos < min2)
				min2 = pos - last_min_pos
			last_min_pos = pos
		}
		++pos;
	}

	println(min2)
}


fun main(args: Array<String>) {
	readLine()  // skip
	var min1: Int = 1000 * 1000 * 1000 + 1
	var min2: Int = 100000 + 1
	var last_min_pos = 0

	var pos = 0
	for(element in (readLine()!!.split(' ').map{it.toInt()})) {
		if(element < min1) {
			min1 = element
			last_min_pos = pos
		} else if(element == min1) {
			if(pos - last_min_pos < min2)
				min2 = pos - last_min_pos
			last_min_pos = pos
		}
		++pos;
	}

	println(min2)
}


import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.math.abs
import kotlin.math.min

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    val n = nextInt()
    val array = IntArray(n, { nextInt() })
    val min = array.min()
    var res = 999999999
    var idx = res
    array.forEachIndexed { i, v ->
        when {
            v.equals(min) -> {
                res = min(res, abs(i - idx))
                idx = i
            }
        }
    }
    print("$res")
}


import kotlin.math.min

fun main() {
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    readLine()
    val arr = readInts()
    var lastMinPos = 0
    var sol = Int.MAX_VALUE
    for (pos in 1 until arr.size)
        when {
            arr[pos] < arr[lastMinPos] -> {
                sol = Int.MAX_VALUE
                lastMinPos = pos
            }
            arr[pos] == arr[lastMinPos] -> {
                sol = min(sol, pos - lastMinPos)
                lastMinPos = pos
            }
        }
    print(sol)
}


import kotlin.math.min

fun main() {
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    readLine()
    val arr = readInts()
    var lastMinPos = 0
    var sol = Int.MAX_VALUE
    for (pos in 1 until arr.size)
        when {
            arr[pos] < arr[lastMinPos] -> lastMinPos = pos
            arr[pos] == arr[lastMinPos] -> {
                sol = min(sol, pos - lastMinPos)
                lastMinPos = pos
            }
        }
    print(sol)
}


import java.util.*

class A {
    fun start(scan: Scanner) {
        var minnum = Int.MAX_VALUE
        var lastMinimumIndex = -1
        var minLenth = Int.MAX_VALUE
        repeat(scan.nextInt()) { index ->
            val current = scan.nextInt()
            if(current < minnum){
                minnum = current
                lastMinimumIndex = index
                minLenth = Int.MAX_VALUE
            } else if (current == minnum) {
                if(index - lastMinimumIndex < minLenth)
                    minLenth = index - lastMinimumIndex
                lastMinimumIndex = index
            }
        }

        println(minLenth)
    }
}

fun main(args: Array<String>) {
    A().start(Scanner(System.`in`))
}


fun main(argv: Array<String>) {
    var n = readLine()?.toInt() ?: 0
    val numbers = readLine()?.split(' ')?.mapNotNull { it.toLong() }!!.toLongArray()

    val min = numbers.min()
    val minIndexes = numbers.indices.filter { numbers[it] == min }

    val answer = (0 .. minIndexes.count() - 2)
            .map { minIndexes[it + 1] - minIndexes[it] }
            .min()

    print(answer)
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define mini(x,y) (x)<(y)?(x):(y)
int arr[100001];
int main()
{
	int n;
	scanf("%d",&n);
	int i,j,k,min=987654321;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		min=mini(min,arr[i]);
	}
	int lastpos=-1;
	k=987654321;
	for(i=0;i<n;i++)
	{
		if(arr[i]==min)
		{
			if(lastpos==-1)
			 lastpos=i;
			else
			 {
			 	k=mini(k,i-lastpos);
			 	lastpos=i;
			 }
		}
	}
	printf("%d\n",k);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define mini(x,y) (x)<(y)?(x):(y)
int arr[100001];
int main()
{
	int n;
	scanf("%d",&n);
	int i,j,k,min=1000000000;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		min=mini(min,arr[i]);
	}
	int lastpos=-1;
	k=1000000000;
	for(i=0;i<n;i++)
	{
		if(arr[i]==min)
		{
			if(lastpos==-1)
			 lastpos=i;
			else
			 {
			 	k=mini(k,i-lastpos);
			 	lastpos=i;
			 }
		}
	}
	printf("%d\n",k);
}


#include<stdio.h>
int main()
{
    int i,j=0,k,n;
    scanf("%d",&n);
    int ar[n+5];
    for(i=0;i<n;i++)
    {
        scanf("%d",&ar[i]);
    }
    int max=ar[0];
    for(i=0;i<n;i++)
    {
        if(max>ar[i])
            {
            max=ar[i];
            k=i;
            }

    }
    int ara[n];
    for(i=0;i<n;i++)
    {
        if(max==ar[i])
            {
                ara[j]=i-k;
                k=i;
                j++;
            }
    }
    int min=100000;
    for(i=1;i<j;i++)
    {
        if (min>ara[i])
            min=ara[i];
    }
    printf("%d\n",min);
    return 0;
}


#include <stdio.h>
main()
{
    int n,i,j,k;
    scanf("%d",&n);
    long long int a[n],b[n],min;;
    scanf("%I64d",&a[0]);
    min=a[0];
    for(i=1;i<n;i++)
    {scanf("%I64d",&a[i]);
    if(a[i]<min) min=a[i];}
    j=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==min) j++;
    }
    int ind[j];
    j=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==min)
        {
            ind[j]=i;
            j++;
        }
    }
    int dif[j-1];
    dif[0]=ind[1]-ind[0];
    min=dif[0];
    for(i=0;i<j-1;i++)
    {
        dif[i]=ind[i+1]-ind[i];
        if(dif[i]<min) min=dif[i];
    }
    printf("%d",min);
}


#include <stdio.h>
main()
{
    int n,i,j;
    scanf("%d",&n);
    long long int a[n],b[n],min;;
    scanf("%I64d",&a[0]);
    min=a[0];
    for(i=1;i<n;i++)
    {scanf("%I64d",&a[i]);
    if(a[i]<min) min=a[i];}
    j=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==min) j++;
    }
    int ind[j];
    j=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==min)
        {
            ind[j]=i;
            j++;
        }
    }
    int dif[j-1];
    dif[0]=ind[1]-ind[0];
    min=dif[0];
    for(i=0;i<j-1;i++)
    {
        dif[i]=ind[i+1]-ind[i];
        if(dif[i]<min) min=dif[i];
    }
    printf("%d",min);
}


#include<stdio.h>
#include<math.h>
int main()
{
	int n,a[100000];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&a[i]);
	int min1=a[0],k1,min2=a[0],k2,min3=a[0],k3;
	for(int i=0;i<n;i++)
	{
		if(min1>a[i])
		{
			min1=a[i];k1=i;
		}
	}
	for(int i=0;i<n;i++)
	{
		if(i==k1)continue;
		else
		{
			if(min2>a[i]){
				min2=a[i],k2=i;
			}
		}
	}
		for(int i=0;i<n;i++)
	{
		if(i==k1||i==k2)continue;
		else
		{
			if(min3>a[i]){
				min3=a[i],k3=i;
			}
		}
	}
	int s,b,c;
	s=abs(k1-k2);
	b=abs(k1-k3);
	c=abs(k2-k3);
	int t;
	if(s>b)t=b;
	else t=s;
	if(t>c)t=c;
	printf("%d",t);
	return 0;
 }


#include <stdio.h>
#define N 10000
int main()
{
	int i,n,a,nflag=0;
	long long an[N],bn[N],time[N];
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&an[i]);
		bn[i]=an[i];
	}

	for(i=0;i<n;i++)
	{
    	for(int j=i+1;j<n;j++)
    	{

	    	if(an[j]<an[i])
	    	{
		    	bn[i]=an[j];
	     	}
	    }
	}

	for(i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
    	{
	    	if(an[j]==bn[i])
	    	{
			    a=0;
			    int b=j;
	    		for(int k=j+1;k<n;k++)
	    		{
	    			if(an[k]==an[j])
	    	  		{
	    	  			nflag=1;
	    		    	time[a]=k-b;
	    		    	b=k;
						a++;
					}
				}
		    if(nflag==1)
	            break;
	     	}
			if(nflag==1)
	            break;
	    }
	    if(nflag==1)
	        break;
	}

	if(nflag==1)
    {
    	int min=time[0];
    	for(int i=0;i<a;i++)
    	{
	        if(min>time[i])
	            min=time[i];
    	}
		printf("%lld",min);
	}
	return 0;
}


#include <stdio.h>
#define N 100005
int main()
{
	int i,n,a,nflag=0;
	long long an[N],time[N];
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&an[i]);
	}
	long long min1=an[0];
	for(i=0;i<n;i++)
	{
    	if(an[i]<min1)
    	   min1=an[i];
	}

	for(i=0;i<n;i++)
	{
	    if(an[i]==min1)
	    {
			a=0;
		    int b=i;
	    	for(int k=i+1;k<n;k++)
	    	{
	    		if(an[k]==an[i])
	    	  	{
	    	  		nflag=1;
	    		    time[a]=k-b;
	    		    b=k;
					a++;
				}
			}
	    }
	    if(nflag==1)
	        break;
	}

	if(nflag==1)
    {
    	long long min=time[0];
    	for(int i=0;i<a;i++)
    	{
	        if(min>time[i])
	            min=time[i];
    	}
		printf("%lld",min);
	}
	return 0;
}


#include <stdio.h>
#define N 100005
int main()
{
	int i,n,a,nflag=0;
	long long an[N],bn[N],time[N];
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&an[i]);
		bn[i]=an[i];
	}

	for(i=0;i<n;i++)
	{
    	for(int j=i+1;j<n;j++)
    	{

	    	if(an[j]<an[i])
	    	{
		    	bn[i]=an[j];
	     	}
	    }
	}

	for(i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
    	{
	    	if(an[j]==bn[i])
	    	{
			    a=0;
			    int b=j;
	    		for(int k=j+1;k<n;k++)
	    		{
	    			if(an[k]==an[j])
	    	  		{
	    	  			nflag=1;
	    		    	time[a]=k-b;
	    		    	b=k;
						a++;
					}
				}
		    if(nflag==1)
	            break;
	     	}
			if(nflag==1)
	            break;
	    }
	    if(nflag==1)
	        break;
	}

	if(nflag==1)
    {
    	long long min=time[0];
    	for(int i=0;i<a;i++)
    	{
	        if(min>time[i])
	            min=time[i];
    	}
		printf("%lld",min);
	}
	return 0;
}


#include<stdio.h>
int main()
{
    int n,i,x,j,count=0,m,a[100000],min=100000009;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        if(a[i]<min)
            min=a[i];

    }
    for(i=0; i<n; i++)
    {
        if(min==a[i])
        {
            x=i;
            break;
        }
    }
    for(i=x+1;i<n;i++)
    {
        if(a[i]==min)
        {
            count=i-x;
            x=i;

        }
    }

   printf("%d",count);
    return 0;
}


#include <stdio.h>
#define MAX 1000000007

int main()
{
	int n, x, i, min = MAX, p, ans = MAX;
	scanf("%d", &n);
	for(i=0;i<n;++i)
	{
		scanf("%d", &x);
		if(min > x)
		{
			min = x;
			p = i;
			ans = MAX;
		}
		else if(min == x)
		{
			ans = ans > (i - p) ? (i - p) : ans;
			p = i;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include<stdio.h>
main()
{
  int n,min,j;
  scanf("%d",&n);
  int a[n],i,k,count=100001;
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  min=a[0];
  for(i=0;i<n;i++)
  {
    if(min>a[i])
    min=a[i];
  }
  for(i=0;i<n;i++)
  {
    if(a[i]==min)
    {
     j=i;
     break;
    }
  }
  for(i=j+1;i<n;i++)
  {
    if(a[i]==min)
    {
      k=i-j;
      j=i;
    }
    if(k<count)
    count=k;
  }
  printf("%d",count);
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,minoc;
    long long a,min=100000000 ;
    scanf("%d",&n);
    while(i<=n){
        scanf("%I64d",&a);
        if(a<=min){
            min=a;
            minoc=nb;
            nb=0;}
        nb++;
        i++;
    }
    printf("%d",minoc);

    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,minoc=100000;
    long long a,min=1000000000;
    scanf("%d",&n);
    /*scanf("%I64d",&a);
    min=a;
    nb=0;*/
    while(i<=n){
        scanf("%I64d",&a);
        if(a<min){
            nb=0;
            min=a;
        }
        else if (a==min)
            {
                if(nb<minoc)
                    minoc=nb;
                nb=0;
            }
            nb++;
        i++;
    }
    printf("%d",minoc);

    return 0;
}


#include <stdio.h>

int main() {
    int n,i=2,nb=1,minoc=100000;
    long long a,min=10000000000;
    scanf("%d",&n);
    scanf("%I64d",&a);
    min=a;
    nb=1;
    while(i<=n){
        scanf("%I64d",&a);
        if(a<min){
            nb=1;
            min=a;
        }
        else if (a==min)
            {
                if(nb<minoc)
                    minoc=nb;
                nb=1;
            }
            nb++;
        i++;
    }
    printf("%d",minoc);

    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,minoc=100000;
    long long a,min=10000000000;
    scanf("%d",&n);
    while(i<=n){
        scanf("%I64d",&a);
        nb++;
        if(a<min){
            nb=0;
            min=a;
            minoc=100000;
        }
        else if (a==min)
            {
                if(nb<minoc)
                    minoc=nb;
                nb=0;
            }
        i++;
    }
    printf("%d",minoc);

    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,minoc=100000;
    long long a,min=10000000000;
    scanf("%d",&n);
    while(i<=n){
        scanf("%I64d",&a);
        nb++;
        if(a<min){
            nb=0;
            min=a;
        }
        else if (a==min)
            {
                if(nb<minoc)
                    minoc=nb;
                nb=0;
            }
        i++;
    }
    printf("%d",minoc);

    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,minoc=100000;
    long long a,min=10000000000;
    scanf("%d",&n);
    /*scanf("%I64d",&a);
    min=a;
    nb=0;*/
    while(i<=n){
        scanf("%I64d",&a);
        if(a<min){
            nb=0;
            min=a;
        }
        else if (a==min)
            {
                if(nb<minoc)
                    minoc=nb;
                nb=0;
            }
            nb++;
        i++;
    }
    printf("%d",minoc);

    return 0;
}


#include<stdio.h>
#include<limits.h>
int main(){
	int n,x,f=0,dist=INT_MAX,fir;
	long long int arr[100007],min=INT_MAX;
	scanf("%d",&n);
	for(x=0;x<n;x++){
		scanf("%lld",&arr[x]);
		if(arr[x]<min)
			min=arr[x];
	}
	x=0;
	while(arr[x]!=min)
		x++;
	fir=x++;
	while(x<n){
		if(arr[x]==min){
			if(x-fir<dist)
				dist=x-fir;
			fir=x;
		}
		x++;
	}
	printf("%d\n",dist);
}


#include <stdio.h>

void main()
{
	int max = 0;
	scanf("%i", &max);
	int *num_array = malloc(max * sizeof(int));
	scanf("%i", &num_array[0]);
	int smallest = num_array[0];
	int index = 0;
	for (int i = 1; i < max; i++)
	{
		scanf("%i", &num_array[i]);
		if (smallest >= num_array[i])
		{
			smallest = num_array[i];
			index = i;
		}
	}
	int answer = 0;
	int best = -1;
	for (int i = index-1; i >= 0; i--)
	{
		if (num_array[i] != smallest)
			answer++;
		else if(answer < best || best == -1)
		{
			best = answer + 1;
			answer = 0;
		}
	}
	printf("%i", best);
}


#include <stdio.h>

int main()
{
	int max = 0;
	scanf("%i", &max);
	int *num_array = malloc(max * sizeof(int));
	scanf("%i", &num_array[0]);
	int smallest = num_array[0];
	int index = 0;
	for (int i = 1; i < max; i++)
	{
		scanf("%i", &num_array[i]);
		if (smallest >= num_array[i])
		{
			smallest = num_array[i];
			index = i;
		}
	}
	int answer = 0;
	int best = -1;
	for (int i = index-1; i >= 0; i--)
	{
		if (num_array[i] != smallest)
			answer++;
		else if(++answer < best || best == -1)
		{
			best = answer;
			answer = 0;
		}
	}
	printf("%i", best);
	return 0;
}


#include <stdio.h>

int main()
{
	int max = 0;
	scanf("%i", &max);
	int smallest = -1;
	int num = 0;
	int count = 0;
	int best = -1;
	for (int i = 0; i < max; i++)
	{
		scanf("%i", &num);
		if (smallest == -1 || num < smallest)
		{
			smallest = num;
			count = 0;
		}
		else
		{
			count++;
			if (smallest == num && (best == -1 || best > count))
			{
				best = count;
				count = 0;
			}
		}
	}
	printf("%i", best);
	return 0;
}


#include <stdio.h>

int main()
{
	int max = 0;
	scanf("%i", &max);
	int smallest = -1;
	int num = 0;
	int count = 0;
	int best = -1;
	for (int i = 0; i < max; i++)
	{
		scanf("%i", &num);
		if (smallest == -1 || num < smallest)
		{
			smallest = num;
			count = 0;
			best = -1;
		}
		else
		{
			count++;
			if (smallest == num)
			{
				if (best == -1 || best > count)
					best = count;
				count = 0;
			}
		}
	}
	printf("%i", best);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#define MaxNum 100000

int main()
{
    int n,i,Num,Index=0,MinDistance=MaxNum,Min;
    scanf("%d ",&n);
    for(i=0;i<n;i++){
        scanf("%d",&Num);
        if(i==0){
            Min=Num;
        }
        else{
            if(Num<Min){
                Min=Num;
                Index=i;
                MinDistance=MaxNum;
            }
            else if(Num==Min){
                if(i-Index<MinDistance){
                    MinDistance=i-Index;
                }
                Index=i;
            }
        }
    }
    printf("%d\n",MinDistance);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(void) {
	int n;
	scanf("%d", &n);
	int *arr = malloc(n * sizeof(*arr));
	for (int i = 0; i < n; i++) {
		scanf("%d", arr + i);
	}
	int min = arr[0];
	for (int i = 1; i < n; i++) {
		if (arr[i] < min) {
			min = arr[i];
		}
	}
	int min_index = -1, i1 = -1, i2 = -1;
	for (int i = 0; i < n; i++) {
		if (arr[i] == min) {
			if (i1 == -1) {
				i1 = i;
				continue;
			}
			if (i2 == -1) {
				i2 = i;
			} else {
				i1 = i2;
				i2 = i;
			}
			if (min_index == -1 || i2 - i1 < min_index) {
				min_index = i2 - i1;
			}
		}
	}
	printf("%d\n", min_index);
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min, second_min;
	scanf("%d %d", &min, &second_min);
	int min_i = 0, second_i = 1;
	if (second_min < min) {
		int t = min;
		min = second_min;
		second_min = t;

		min_i = 1;
		second_i = 0;
	}
	for (int i = 2; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			second_min = min;
			min = num;

			second_i = min_i;
			min_i = i;
			continue;
		}
		if (num < second_min) {
			second_min = num;
			second_i = i;
			continue;
		}
		if (num == min && abs(second_i - i) < abs(second_i - min_i)) {
			min = num;
			min_i = i;
			continue;
		}
		if (num == second_min && abs(i - min_i) < abs(second_i - min_i)) {
			second_min = num;
			second_i = i;
			continue;
		}

	}
	printf("%d\n", abs(min_i - second_i));
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			continue;
		}
		if (num == min) {
			i2 = i;
		}
	}
	int abs = i1 - i2;
	printf("%d\n", abs < 0 ? -abs : abs);
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min && abs(i - i2) < abs(i2 - i1)) {
			i1 = i2;
			i2 = i;
		}
	}
	printf("%d\n", abs(i1 - i2;));
	return 0;
}


#include <stdio.h>

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min && i - i2 < i2 - i1) {
			i1 = i2;
			i2 = i;
			continue;
		}
	}
	printf("%d\n", i2 - i1);
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min) {
			i1 = i2;
			i2 = i;
		}
	}
	int abs = i1 - i2;
	printf("%d\n", abs < 0 ? -abs : abs);
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min) {
			i1 = i2;
			i2 = i;
		}
	}
	int abs = i1 - i2;
	printf("%d\n", abs < 0 ? -abs : abs);
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min && abs(i - i2) < abs(i2 - i1)) {
			i1 = i2;
			i2 = i;
		}
	}
	printf("%d\n", abs(i1 - i2));
	return 0;
}


#include <stdio.h>

int main(void) {
	int n, min, i1 = 0, i2 = -1;
	scanf("%d %d", &n, &min);
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min && i - i2 < i2 - i1) {
			i1 = i2;
			i2 = i;
			continue;
		}
	}
	if (i2 == -1) {
		return 1;
	}
	printf("%d\n", i2 - i1);
	return 1;
}


#include <stdio.h>

int main(void) {
	int n;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	int i1 = 0, i2 = -1;
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min && i - i2 < i2 - i1) {
			i1 = i2;
			i2 = i;
		}
	}
	printf("%d\n", i2 - i1);
	return 0;
}


#include <stdio.h>

int main(void) {
	int n, min, i1 = 0, i2 = -1;
	scanf("%d %d", &n, &min);
	for (int i = 1; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			min = num;
			i1 = i;
			i2 = -1;
			continue;
		}
		if (num == min && i2 == -1) {
			i2 = i;
			continue;
		}
		if (num == min && i - i2 < i2 - i1) {
			i1 = i2;
			i2 = i;
			continue;
		}
	}
	if (i2 == -1) {
		return 1;
	}
	printf("%d\n", i2 - i1);
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min, second_min;
	scanf("%d %d", &min, &second_min);
	int min_i = 0, second_i = 1;
	if (second_min < min) {
		int t = min;
		min = second_min;
		second_min = t;

		min_i = 1;
		second_i = 0;
	}
	for (int i = 2; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			second_min = min;
			min = num;

			second_i = min_i;
			min_i = i;
			continue;
		}
		if (num < second_min) {
			second_min = num;
			second_i = i;
			continue;
		}
		if (num == second_min && abs(i - min_i) < abs(second_i - min_i)) {
			second_min = num;
			second_i = i;
			continue;
		}
		if (num == min && abs(second_i - i) < abs(second_i - min_i)) {
			min = num;
			min_i = i;
			continue;
		}
	}
	printf("%d\n", abs(min_i - second_i));
	return 0;
}


#include <stdio.h>

int abs(int n) {
	return n < 0 ? -n : n;
}

int main(void) {
	int n;
	scanf("%d", &n);
	int min, second_min;
	scanf("%d %d", &min, &second_min);
	int min_i = 0, second_i = 1;
	if (second_min < min) {
		int t = min;
		min = second_min;
		second_min = t;

		min_i = 1;
		second_i = 0;
	}
	for (int i = 2; i < n; i++) {
		int num;
		scanf("%d", &num);
		if (num < min) {
			second_min = min;
			min = num;

			second_i = min_i;
			min_i = i;
			continue;
		}
		if (num < second_min) {
			second_min = num;
			second_i = i;
			continue;
		}
		if (num == second_min && abs(i - min_i) < abs(second_i - min_i)) {
			second_min = num;
			second_i = i;
			continue;
		}
		if (num == min && abs(second_i - i) < abs(second_i - min_i)) {
			min = num;
			min_i = i;
			continue;
		}
	}
	printf("%d %d\n", min_i, second_i);
	printf("%d\n", abs(min_i - second_i));
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <math.h>

int main()
{
    long long int n;
    scanf("%I64d",&n);
    long long int a[n],min,i,dist=10000000000,b[n],j;
    for(i=0;i<n;i++){
        scanf("%I64d",&a[i]);
    }
    min=a[0];
    for(i=1;i<n;i++){
        if(a[i]<min){
            min=a[i];
        }
    }
    for(i=0,j=0;i<n;i++){
        if(min==a[i]){
            b[j]=i;
            j++;
        }
    }
    for(i=1;i<j;i++){
        if(b[i]-b[i-1]<dist){
            dist=b[i]-b[i-1];
        }
    }
    printf("%I64d",dist);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
  int n;
  scanf("%d",&n);
  long a[n];
  int m;
  long min=0;
  for(m = 0; m < n;m++)
  {
  scanf("%ld",&a[m]);
  if(m==0)
  {
  min=a[0];
  }
  else if(min>a[m])
  min=a[m];
  }
int i1[n];
int k=0;
for(m = 0;m<n;m++)
{
  if(min==a[m])
  i1[k++]=m;
}
int dmin=i1[1]-i1[0];
for(m=0;m<k-1;m++)
{
  if(i1[m+1]-i1[m]<dmin)
  {
  dmin=i1[m+1]-i1[m];
}}
printf("%d\n",dmin);
return 0;
}


#include "stdio.h"
#include<stdlib.h>
void merge(long*,int,int,int);
void mergeSort(long*,int,int);
void merge(long arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(long arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}
int main(void)
{
  int n;
  scanf("%d",&n);
  long a[n],b[n];
  int m,p;
  for(m = 0; m < n;m++)
  {
  scanf("%ld",&a[m]);
  b[m]=a[m];
  }
mergeSort(b,0,n-1);
int i1[n],k=0,dmin;
for(m = 0;m<n;m++)
{
  if(b[0]==a[m])
  i1[k++]=m;
}
dmin=i1[1]-i1[0];
for(m=0;m<k-1;m++)
{
  if(i1[m+1]-i1[m]<dmin)
  dmin=i1[m+1]-i1[m];
}
printf("%d\n",dmin);
return 0;
}


#include<stdio.h>
int main()
{
	int n, min, one, two, i;
	scanf("%d", &n);
	int a[n]; int dist[n];
	for (i = 0; i < n; ++i)
		scanf("%ld", &a[i]);
	min=a[0];
	for (i = 1; i < n; ++i)
		if(a[i]<min) min=a[i];
	i=0; int d=0;
	while(i<n)
	{
		if(a[i]==min)
		{
			one = i++;
			while(i<n)
			{
				if(a[i]==min)
				{
					two = i;
					dist[d++]=two-one;
					break;
				}
				else i++;
			}
		}
		else i++;
	}
	min=dist[0];
	for (int i = 0; i < d; ++i)
		if(dist[i]<min) min=dist[i];
	printf("%d", min);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned n;
    fscanf( stdin , "%u" , &n );

    unsigned min = 0 - 1;
    unsigned stack[ n ];
    int index = -1;

    unsigned a[ n ];
    for(int i = 0;i < n;i++)
    {
        fscanf( stdin , "%u" , &a[ i ] );

        if(a[ i ] < min)
        {
            min = a[ i ];
            index = -1;
            stack[ ++index ] = i;
        }
        else if(a[ i ] == min)
            stack[ ++index ] = i;
    }
    unsigned short distance = 0 - 1;

    for(int i = 0;i < index;i++)
    {
        if(stack[ i + 1 ] - stack[ i ] < distance)
            distance = stack[ i + 1 ] - stack[ i ];
    }

    printf("%hu\n" , distance );

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned n;
    fscanf( stdin , "%u" , &n );

    unsigned min = 0 - 1;
    unsigned stack[ n ];
    int index = -1;

    unsigned a[ n ];
    for(int i = 0;i < n;i++)
    {
        fscanf( stdin , "%u" , &a[ i ] );

        if(a[ i ] < min)
        {
            min = a[ i ];
            index = -1;
            stack[ ++index ] = i;
        }
        else if(a[ i ] == min)
            stack[ ++index ] = i;
    }
    unsigned distance = 0 - 1;

    for(int i = 0;i < index;i++)
    {
        if(stack[ i + 1 ] - stack[ i ] < distance)
            distance = stack[ i + 1 ] - stack[ i ];
    }

    printf("%u\n" , distance );

    return 0;
}


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e5+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
pa q[2][N];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0,q[0][++cnt]=mkp(i,j);
   			else if(i+j-1==ma) q[0][++cnt]=mkp(i,j);
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm='z'+1;
   		for(int j=1;j<=cnt;j++)
   		{
   			int x=q[c][j].fi+1,y=q[c][j].se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   				else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   			else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   		}
       putchar(mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e6+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
pa q[2][N];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0,q[0][++cnt]=mkp(i,j);
   			else if(i+j-1==ma) q[0][++cnt]=mkp(i,j);
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0][++cnt]=mkp(1,1),putchar(a[1][1]),ma=1;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm=123;
   		for(int j=1;j<=cnt;j++)
   		{
   			int x=q[c][j].fi+1,y=q[c][j].se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   				else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   			else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   		}
       printf("%c",mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e5+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
pa q[2][N];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0,q[0][++cnt]=mkp(i,j);
   			else if(i+j-1==ma) q[0][++cnt]=mkp(i,j);
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0][++cnt]=mkp(1,1),putchar(a[1][1]),ma=1;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm='z'+1;
   		for(int j=1;j<=cnt;j++)
   		{
   			int x=q[c][j].fi+1,y=q[c][j].se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   				else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   			else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   		}
       putchar(mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e6+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
set<pa> q[2];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0, q[0].insert(mkp(i,j));
   			else if(i+j-1==ma) q[0].insert(mkp(i,j));
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0].insert(mkp(1,1)),putchar(a[1][1]),ma=1;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm=123;
   		for(auto e: q[c])
   		{
   			int x=e.fi+1,y=e.se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1].insert(mkp(x,y));
   				else if(a[x][y]==mm) q[c^1].insert(mkp(x,y));
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1].insert(mkp(x,y));
   			else if(a[x][y]==mm) q[c^1].insert(mkp(x,y));
   		}
       putchar(mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e5+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
pa q[2][N];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0,q[0][++cnt]=mkp(i,j);
   			else if(i+j-1==ma) q[0][++cnt]=mkp(i,j);
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0][++cnt]=mkp(1,1),putchar(a[1][1]);
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm='z'+1;
   		for(int j=1;j<=cnt;j++)
   		{
   			int x=q[c][j].fi+1,y=q[c][j].se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   				else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   			else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   		}
       putchar(mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e5+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
pa q[2][N];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0,q[0][++cnt]=mkp(i,j);
   			else if(i+j-1==ma) q[0][++cnt]=mkp(i,j);
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0][++cnt]=mkp(1,1),putchar(a[1][1]),ma=1;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm=123;
   		for(int j=1;j<=cnt;j++)
   		{
   			int x=q[c][j].fi+1,y=q[c][j].se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   				else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   			else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   		}
       putchar(mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e6+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
set<pa> q[2];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,q[0].clear(),q[0].insert(mkp(i,j));
   			else if(i+j-1==ma) q[0].insert(mkp(i,j));
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0].insert(mkp(1,1)),putchar(a[1][1]),ma=1;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		char mm=123;
   		for(auto e: q[c])
   		{
   			int x=e.fi+1,y=e.se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],q[c^1].clear(),q[c^1].insert(mkp(x,y));
   				else if(a[x][y]==mm) q[c^1].insert(mkp(x,y));
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],q[c^1].clear(),q[c^1].insert(mkp(x,y));
   			else if(a[x][y]==mm) q[c^1].insert(mkp(x,y));
   		}
       putchar(mm);
       c^=1;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll  long long
//#define int long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
const int N=2e5+10;
const int mod=998244353;
using namespace std;

char a[2020][2020];
pa q[2][N];
int cnt,ma,n,k,dp[2020][2020];
int32_t main()
{
   //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    scanf("%d%d",&n,&k);
   for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
   for(int i=1;i<=n;++i)
   	for(int j=1;j<=n;j++)
   	{
   	    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+(a[i][j]=='a');
   		if(dp[i][j]+k==i+j-1)
   		{
   			if(i+j-1>ma) ma=i+j-1,cnt=0,q[0][++cnt]=mkp(i,j);
   			else if(i+j-1==ma) q[0][++cnt]=mkp(i,j);
   		}
   	}
   	if(dp[n][n]+k>=2*n-1) ma=2*n-1;
   	for(int i=1;i<=ma;i++) putchar('a');
   	int c=0;
    if(ma==0) q[0][++cnt]=mkp(1,1),putchar(a[1][1]),ma=1;
   	for(int i=ma+1;i<=2*n-1;i++)
   	{
   		int cc=0;
   		char mm=123;
   		for(int j=1;j<=cnt;j++)
   		{
   			int x=q[c][j].fi+1,y=q[c][j].se;
   			if(x<=n&&y<=n)
   			{
   				if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   				else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   			}
   			x--,y++;
   			if(x>n||y>n) continue;
   			if(a[x][y]<mm) mm=a[x][y],cc=0,q[c^1][++cc]=mkp(x,y);
   			else if(a[x][y]==mm) q[c^1][++cc]=mkp(x,y);
   		}
       printf("%c",mm);
       c^=1;cnt=cc;
   	}
return 0;
}
/*

*/


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e3;
const int inf=1e9;
int dis[N+2][N+2];
char mat[N+2][N+2];
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n,k;cin>>n>>k;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    for(int j=1;j<=n;j++)
      mat[i][j]=s[j-1];
  }
  for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
      dis[i][j]=inf;
  int ds=1;
  if(mat[1][1]=='a')ds=0;
  set<pair<int,pair<int,int> > >st;st.insert({ds,{1,1}});

  int mx=0;
  while(st.size())
  {
    pair<int,pair<int,int> >p=*st.begin();
    st.erase(p);dis[p.second.first][p.second.second]=p.first;

    if(dis[p.second.first][p.second.second]==k)
      mx=max(mx,p.second.first+p.second.second);

    if(p.second.first+1<=n&&dis[p.second.first+1][p.second.second]==inf)
    {
      if(mat[p.second.first+1][p.second.second]=='a')
        dis[p.second.first+1][p.second.second]=p.first;
      else dis[p.second.first+1][p.second.second]=p.first+1;
      st.insert({dis[p.second.first+1][p.second.second],{p.second.first+1,p.second.second}});
    }
    if(p.second.second+1<=n&&dis[p.second.first][p.second.second+1]==inf)
    {
      if(mat[p.second.first][p.second.second+1]=='a')
        dis[p.second.first][p.second.second+1]=p.first;
      else dis[p.second.first][p.second.second+1]=p.first+1;
      st.insert({dis[p.second.first][p.second.second+1],{p.second.first,p.second.second+1}});
    }
  }
  vector<pair<int,int> >v;
  for(int i=1;i<=n;i++)
  {
    if(i<mx)
    {
      if(dis[i][mx-i]==k)
        v.push_back({i,mx-i});
    }
  }
  mx--;
  while(mx>0)
  {
    cout<<'a';mx--;
  }
  queue<pair<int,int> >q1,q2;
  for(auto x:v)
  {
    if(x.first+1<=n)
      q1.push({x.first+1,x.second});
    if(x.second+1<=n)
      q1.push({x.first,x.second+1});
  }
  if(q1.size()==0)q1.push({1,1});
  while(true)
  {
    q2=q1;char mn='z';
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      mn=min(mn,mat[p.first][p.second]);
    }
    while(q2.size())
    {
      pair<int,int>p=q2.front();q2.pop();
      if(mat[p.first][p.second]==mn)
      {
        q1.push(p);
      }
    }
    cout<<mn;
    set<pair<int,int> >st;
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      if(p.first+1<=n)
        st.insert({p.first+1,p.second});
      if(p.second+1<=n)
        st.insert({p.first,p.second+1});
    }
    while(st.size())
    {
      pair<int,int>p=*st.begin();
      q1.push(p);st.erase(p);
    }
    if(q1.size()==0)break;
  }
  cout<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e3;
const int inf=1e9;
int dis[N+2][N+2];
char mat[N+2][N+2];
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n,k;cin>>n>>k;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    for(int j=1;j<=n;j++)
      mat[i][j]=s[j-1];
  }
  for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
      dis[i][j]=inf;
  int ds=1;
  if(mat[1][1]=='a')ds=0;
  set<pair<int,pair<int,int> > >st;st.insert({ds,{1,1}});

  int mx=0;
  while(st.size())
  {
    pair<int,pair<int,int> >p=*st.begin();
    st.erase(p);dis[p.second.first][p.second.second]=p.first;

    if(dis[p.second.first][p.second.second]==k)
      mx=max(mx,p.second.first+p.second.second);

    if(p.second.first+1<=n&&dis[p.second.first+1][p.second.second]==inf)
    {
      if(mat[p.second.first+1][p.second.second]=='a')
        dis[p.second.first+1][p.second.second]=p.first;
      else dis[p.second.first+1][p.second.second]=p.first+1;
      st.insert({dis[p.second.first+1][p.second.second],{p.second.first+1,p.second.second}});
    }
    if(p.second.second+1<=n&&dis[p.second.first][p.second.second+1]==inf)
    {
      if(mat[p.second.first][p.second.second+1]=='a')
        dis[p.second.first][p.second.second+1]=p.first;
      else dis[p.second.first][p.second.second+1]=p.first+1;
      st.insert({dis[p.second.first][p.second.second+1],{p.second.first,p.second.second+1}});
    }
  }
//  for(int i=1;i<=n;i++)
//  {
//    for(int j=1;j<=n;j++)
//    {
//      cout<<dis[i][j]<<" ";
//    }
//    cout<<endl;
//  }
  //cout<<mx<<endl;

  vector<pair<int,int> >v;
  for(int i=1;i<=n;i++)
  {
    if(i>mx)
    {
      if(dis[i][mx-i]==k&&mx)
        v.push_back({i,mx-i});
    }
  }
//  for(auto x:v)
//    cout<<x.first<<" "<<x.second<<endl;

  mx--;
  while(mx>0)
  {
    cout<<'a';mx--;
  }
  //cout<<mx<<endl;

  queue<pair<int,int> >q1,q2;
  for(auto x:v)
  {
    if(x.first+1<=n)
      q1.push({x.first+1,x.second});
    if(x.second+1<=n)
      q1.push({x.first,x.second+1});
  }
 // cout<<q1.size()<<endl;
  if(q1.size()==0)q1.push({1,1});

  while(true)
  {
    q2=q1;char mn='z';
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      mn=min(mn,mat[p.first][p.second]);
    }
    while(q2.size())
    {
      pair<int,int>p=q2.front();q2.pop();
      if(mat[p.first][p.second]==mn)
      {
        q1.push(p);
      }
    }
    cout<<mn;
    set<pair<int,int> >st;
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      if(p.first+1<=n)
        st.insert({p.first+1,p.second});
      if(p.second+1<=n)
        st.insert({p.first,p.second+1});
    }
    while(st.size())
    {
      pair<int,int>p=*st.begin();
      q1.push(p);st.erase(p);
    }
    if(q1.size()==0)break;
  }
  cout<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e3;
const int inf=1e9;
int dis[N+2][N+2];
char mat[N+2][N+2];
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n,k;cin>>n>>k;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    for(int j=1;j<=n;j++)
      mat[i][j]=s[j-1];
  }
  for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
      dis[i][j]=inf;
  int ds=1;
  if(mat[1][1]=='a')ds=0;
  set<pair<int,pair<int,int> > >st;st.insert({ds,{1,1}});

  int mx=0;
  while(st.size())
  {
    pair<int,pair<int,int> >p=*st.begin();
    st.erase(p);dis[p.second.first][p.second.second]=p.first;

    if(dis[p.second.first][p.second.second]==k)
      mx=max(mx,p.second.first+p.second.second);

    if(p.second.first+1<=n&&dis[p.second.first+1][p.second.second]==inf)
    {
      if(mat[p.second.first+1][p.second.second]=='a')
        dis[p.second.first+1][p.second.second]=p.first;
      else dis[p.second.first+1][p.second.second]=p.first+1;
      st.insert({dis[p.second.first+1][p.second.second],{p.second.first+1,p.second.second}});
    }
    if(p.second.second+1<=n&&dis[p.second.first][p.second.second+1]==inf)
    {
      if(mat[p.second.first][p.second.second+1]=='a')
        dis[p.second.first][p.second.second+1]=p.first;
      else dis[p.second.first][p.second.second+1]=p.first+1;
      st.insert({dis[p.second.first][p.second.second+1],{p.second.first,p.second.second+1}});
    }
  }
  vector<pair<int,int> >v;
  for(int i=1;i<=n;i++)
  {
    if(i<mx)
    {
      if(dis[i][mx-i]==k)
        v.push_back({i,mx-i});
    }
  }
  if(mx==0&&k)
  {
    for(int i=1;i<=2*n-1;i++)
      cout<<'a';
    cout<<endl;
    exit(0);
  }
  mx--;
  while(mx>0)
  {
    cout<<'a';mx--;
  }
  queue<pair<int,int> >q1,q2;
  for(auto x:v)
  {
    if(x.first+1<=n)
      q1.push({x.first+1,x.second});
    if(x.second+1<=n)
      q1.push({x.first,x.second+1});
  }
  if(q1.size()==0)q1.push({1,1});
  while(true)
  {
    q2=q1;char mn='z';
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      mn=min(mn,mat[p.first][p.second]);
    }
    while(q2.size())
    {
      pair<int,int>p=q2.front();q2.pop();
      if(mat[p.first][p.second]==mn)
      {
        q1.push(p);
      }
    }
    cout<<mn;
    set<pair<int,int> >st;
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      if(p.first+1<=n)
        st.insert({p.first+1,p.second});
      if(p.second+1<=n)
        st.insert({p.first,p.second+1});
    }
    while(st.size())
    {
      pair<int,int>p=*st.begin();
      q1.push(p);st.erase(p);
    }
    if(q1.size()==0)break;
  }
  cout<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e3;
const int inf=1e9;
int dis[N+2][N+2];
char mat[N+2][N+2];
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n,k;cin>>n>>k;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    for(int j=1;j<=n;j++)
      mat[i][j]=s[j-1];
  }
  for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
      dis[i][j]=inf;
  int ds=1;
  if(mat[1][1]=='a')ds=0;
  set<pair<int,pair<int,int> > >st;st.insert({ds,{1,1}});

  int mx=0;
  while(st.size())
  {
    pair<int,pair<int,int> >p=*st.begin();
    st.erase(p);dis[p.second.first][p.second.second]=p.first;

    if(dis[p.second.first][p.second.second]==k)
      mx=max(mx,p.second.first+p.second.second);

    if(p.second.first+1<=n&&dis[p.second.first+1][p.second.second]==inf)
    {
      if(mat[p.second.first+1][p.second.second]=='a')
        dis[p.second.first+1][p.second.second]=p.first;
      else dis[p.second.first+1][p.second.second]=p.first+1;
      st.insert({dis[p.second.first+1][p.second.second],{p.second.first+1,p.second.second}});
    }
    if(p.second.second+1<=n&&dis[p.second.first][p.second.second+1]==inf)
    {
      if(mat[p.second.first][p.second.second+1]=='a')
        dis[p.second.first][p.second.second+1]=p.first;
      else dis[p.second.first][p.second.second+1]=p.first+1;
      st.insert({dis[p.second.first][p.second.second+1],{p.second.first,p.second.second+1}});
    }
  }
  vector<pair<int,int> >v;
  for(int i=1;i<=n;i++)
  {
    if(i<mx)
    {
      if(dis[i][mx-i]==k)
        v.push_back({i,mx-i});
    }
  }
  if(dis[n][n]<=k)
  {
    for(int i=1;i<=2*n-1;i++)
      cout<<'a';
    cout<<endl;
    exit(0);
  }
  mx--;
  while(mx>0)
  {
    cout<<'a';mx--;
  }
  queue<pair<int,int> >q1,q2;
  for(auto x:v)
  {
    if(x.first+1<=n)
      q1.push({x.first+1,x.second});
    if(x.second+1<=n)
      q1.push({x.first,x.second+1});
  }
  if(q1.size()==0)q1.push({1,1});
  while(true)
  {
    q2=q1;char mn='z';
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      mn=min(mn,mat[p.first][p.second]);
    }
    while(q2.size())
    {
      pair<int,int>p=q2.front();q2.pop();
      if(mat[p.first][p.second]==mn)
      {
        q1.push(p);
      }
    }
    cout<<mn;
    set<pair<int,int> >st;
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      if(p.first+1<=n)
        st.insert({p.first+1,p.second});
      if(p.second+1<=n)
        st.insert({p.first,p.second+1});
    }
    while(st.size())
    {
      pair<int,int>p=*st.begin();
      q1.push(p);st.erase(p);
    }
    if(q1.size()==0)break;
  }
  cout<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e3;
char mat[N+2][N+2];
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n,k;cin>>n>>k;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    for(int j=1;j<=n;j++)
      mat[i][j]=s[j-1];
  }
  queue<pair<int,int> >q1,q2;q1.push({1,1});
  int loop=2*n-1;
  while(loop--)
  {
    q2=q1;char mn='z';
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      mn=min(mn,mat[p.first][p.second]);
    }
    if(mn=='a')
    {
      while(q2.size())
      {
        pair<int,int>p=q2.front();q2.pop();
        if(mat[p.first][p.second]==mn)
        {
          q1.push(p);
        }
      }
      cout<<'a';
    }
    else if(k)
    {
      k--;cout<<'a';
      q1=q2;
    }
    else
    {
      while(q2.size())
      {
        pair<int,int>p=q2.front();q2.pop();
        if(mat[p.first][p.second]==mn)
        {
          q1.push(p);
        }
      }
      cout<<mn;
    }
    set<pair<int,int> >st;
    while(q1.size())
    {
      pair<int,int>p=q1.front();q1.pop();
      if(p.first+1<=n)
        st.insert({p.first+1,p.second});
      if(p.second+1<=n)
        st.insert({p.first,p.second+1});
    }
    while(st.size())
    {
      pair<int,int>p=*st.begin();
      q1.push(p);st.erase(p);
    }
  }
  cout<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define LOR(i,l,r) for (ll i=l;i<=r;i++)
#define FORD(i,r,l) for (int i=r;i>=l;i--)
#define LORD(i,r,l) for (ll i=r;i>=l;i--)
#define INF 1000000000
#define CL(x) memset(x,0,sizeof(x))
typedef long long ll;

int N,K;
#define MXN 2005
string M[MXN];
int Use[MXN][MXN];
bool vis[MXN][MXN];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>N>>K;
    FOR(i,0,N-1)
    {
        cin>>M[i];
        FOR(j,0,N-1)Use[i][j]=INF;
    }
    //use best to find best
    queue<pii> q;
    q.push( mkp(0,0) );
    Use[0][0]=0;
    vis[0][0]=true;
    if(M[0][0]!='a')
    {
        Use[0][0]=1;//update using min dis
    }
    //BFS1
    while(!q.empty())
    {
        int cx=q.front().f,cy=q.front().s;
        q.pop();
        int d = Use[cx][cy],nd;
        if(cx!=N-1)
        {
            if(M[cx+1][cy]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx+1][cy] )
            {
                Use[cx+1][cy]=nd;
                if(!vis[cx+1][cy])
                {
                    vis[cx+1][cy]=true;
                    q.push(mkp(cx+1,cy));
                }
            }
        }
        if(cy!=N-1)
        {
            if(M[cx][cy+1]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx][cy+1] )
            {
                Use[cx][cy+1]=nd;
                if(!vis[cx][cy+1])
                {
                    vis[cx][cy+1]=true;
                    q.push(mkp(cx,cy+1));
                }
            }
        }
    }
    FOR(i,0,N-1)FOR(j,0,N-1)if(Use[i][j]<=K)M[i][j]='a';
    CL(vis);
    q.push(mkp(0,0));
    FOR(c,0,2*N-2)
    {
        int cx=q.front().f,cy=q.front().s;
        cout<<M[cx][cy];
        int Min=100;
        vector<pii> Nxt;
        while( !q.empty() )
        {
            int cx=q.front().f,cy=q.front().s;
            q.pop();
            if( cx!=N-1 )
            {
                if( M[cx+1][cy]-'a' <= Min )
                {
                    Min=M[cx+1][cy]-'a';
                    if(!vis[cx+1][cy])
                    {
                        vis[cx+1][cy]=true;
                        Nxt.pb(mkp(cx+1,cy));
                    }
                }
            }
            if( cy!=N-1 )
            {
                if( M[cx][cy+1]-'a' <= Min )
                {
                    Min=M[cx][cy+1]-'a';
                    if(!vis[cx][cy+1])
                    {
                        vis[cx][cy+1]=true;
                        Nxt.pb(mkp(cx,cy+1));
                    }
                }
            }
        }
        for(pii Nxi : Nxt)
        {
            if( M[Nxi.f][Nxi.s]-'a'==Min )q.push(mkp(Nxi.f,Nxi.s));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define LOR(i,l,r) for (ll i=l;i<=r;i++)
#define FORD(i,r,l) for (int i=r;i>=l;i--)
#define LORD(i,r,l) for (ll i=r;i>=l;i--)
#define INF 1000000000
#define CL(x) memset(x,0,sizeof(x))
typedef long long ll;

int N,K;
#define MXN 2005
string M[MXN];
int Use[MXN][MXN];
bool vis[MXN][MXN];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>N>>K;
    FOR(i,0,N-1)
    {
        cin>>M[i];
        FOR(j,0,N-1)Use[i][j]=INF;
    }
    //use best to find best
    queue<pii> q;
    q.push( mkp(0,0) );
    Use[0][0]=0;
    vis[0][0]=true;
    if(M[0][0]!='a'&&K>0)
    {
        Use[0][0]=1;//update using min dis
    }
    //BFS1
    while(!q.empty())
    {
        int cx=q.front().f,cy=q.front().s;
        q.pop();
        int d = Use[cx][cy],nd;
        if(cx!=N-1)
        {
            if(M[cx+1][cy]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx+1][cy] )
            {
                Use[cx+1][cy]=nd;
                if(!vis[cx+1][cy])
                {
                    vis[cx+1][cy]=true;
                    q.push(mkp(cx+1,cy));
                }
            }
        }
        if(cy!=N-1)
        {
            if(M[cx][cy+1]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx][cy+1] )
            {
                Use[cx][cy+1]=nd;
                if(!vis[cx][cy+1])
                {
                    vis[cx][cy+1]=true;
                    q.push(mkp(cx,cy+1));
                }
            }
        }
    }
    FOR(i,0,N-1)FOR(j,0,N-1)if(Use[i][j]<=K)M[i][j]='a';
    q.push(mkp(0,0));
    FOR(c,0,2*N-2)
    {
        int cx=q.front().f,cy=q.front().s;
        cout<<M[cx][cy];
        int Min=100;
        vector<pii> Nxt;
        while( !q.empty() )
        {
            int cx=q.front().f,cy=q.front().s;
            q.pop();
            if( cx!=N-1 )
            {
                if( M[cx+1][cy]-'a' < Min )
                {
                    Min=M[cx+1][cy]-'a';
                    Nxt.pb(mkp(cx+1,cy));
                }
            }
            if( cy!=N-1 )
            {
                if( M[cx][cy+1]-'a' < Min )
                {
                    Min=M[cx][cy+1]-'a';
                    Nxt.pb(mkp(cx,cy+1));
                }
            }
        }
        for(pii Nxi : Nxt)
        {
            if( M[Nxi.f][Nxi.s]-'a'==Min )q.push(mkp(Nxi.f,Nxi.s));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define LOR(i,l,r) for (ll i=l;i<=r;i++)
#define FORD(i,r,l) for (int i=r;i>=l;i--)
#define LORD(i,r,l) for (ll i=r;i>=l;i--)
#define INF 1000000000
#define CL(x) memset(x,0,sizeof(x))
typedef long long ll;

int N,K;
#define MXN 2005
string M[MXN];
int Use[MXN][MXN];
bool vis[MXN][MXN];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>N>>K;
    FOR(i,0,N-1)
    {
        cin>>M[i];
        FOR(j,0,N-1)Use[i][j]=INF;
    }
    //use best to find best
    queue<pii> q;
    q.push( mkp(0,0) );
    Use[0][0]=0;
    vis[0][0]=true;
    if(M[0][0]!='a'&&K>0)
    {
        Use[0][0]=1;//update using min dis
    }
    //BFS1
    while(!q.empty())
    {
        int cx=q.front().f,cy=q.front().s;
        q.pop();
        int d = Use[cx][cy],nd;
        if(cx!=N-1)
        {
            if(M[cx+1][cy]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx+1][cy] )
            {
                Use[cx+1][cy]=nd;
                if(!vis[cx+1][cy])
                {
                    vis[cx+1][cy]=true;
                    q.push(mkp(cx+1,cy));
                }
            }
        }
        if(cy!=N-1)
        {
            if(M[cx][cy+1]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx][cy+1] )
            {
                Use[cx][cy+1]=nd;
                if(!vis[cx][cy+1])
                {
                    vis[cx][cy+1]=true;
                    q.push(mkp(cx,cy+1));
                }
            }
        }
    }
    FOR(i,0,N-1)FOR(j,0,N-1)if(Use[i][j]<=K)M[i][j]='a';

    q.push(mkp(0,0));
    FOR(c,0,2*N-2)
    {
        int cx=q.front().f,cy=q.front().s;
        cout<<M[cx][cy];
        int Min=100;
        vector<pii> Nxt;
        while( !q.empty() )
        {
            int cx=q.front().f,cy=q.front().s;
            q.pop();
            if( cx!=N-1 )
            {
                if( M[cx+1][cy]-'a' <= Min )
                {
                    Min=M[cx+1][cy]-'a';
                    Nxt.pb(mkp(cx+1,cy));
                }
            }
            if( cy!=N-1 )
            {
                if( M[cx][cy+1]-'a' <= Min )
                {
                    Min=M[cx][cy+1]-'a';
                    Nxt.pb(mkp(cx,cy+1));
                }
            }
        }
        for(pii Nxi : Nxt)
        {
            if( M[Nxi.f][Nxi.s]-'a'==Min )q.push(mkp(Nxi.f,Nxi.s));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define LOR(i,l,r) for (ll i=l;i<=r;i++)
#define FORD(i,r,l) for (int i=r;i>=l;i--)
#define LORD(i,r,l) for (ll i=r;i>=l;i--)
#define INF 1000000000
#define CL(x) memset(x,0,sizeof(x))
typedef long long ll;

int N,K;
#define MXN 2005
string M[MXN];
int Use[MXN][MXN];
bool vis[MXN][MXN];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>N>>K;
    FOR(i,0,N-1)
    {
        cin>>M[i];
        FOR(j,0,N-1)Use[i][j]=INF;
    }
    //use best to find best
    queue<pii> q;
    q.push( mkp(0,0) );
    Use[0][0]=0;
    vis[0][0]=true;
    if(M[0][0]!='a')
    {
        Use[0][0]=1;//update using min dis
    }
    //BFS1
    while(!q.empty())
    {
        int cx=q.front().f,cy=q.front().s;
        q.pop();
        int d = Use[cx][cy],nd;
        if(cx!=N-1)
        {
            if(M[cx+1][cy]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx+1][cy] )
            {
                Use[cx+1][cy]=nd;
                if(!vis[cx+1][cy])
                {
                    vis[cx+1][cy]=true;
                    q.push(mkp(cx+1,cy));
                }
            }
        }
        if(cy!=N-1)
        {
            if(M[cx][cy+1]=='a')nd=d;
            else nd=d+1;
            if( nd<Use[cx][cy+1] )
            {
                Use[cx][cy+1]=nd;
                if(!vis[cx][cy+1])
                {
                    vis[cx][cy+1]=true;
                    q.push(mkp(cx,cy+1));
                }
            }
        }
    }
    FOR(i,0,N-1)FOR(j,0,N-1)if(Use[i][j]<=K)M[i][j]='a';

    q.push(mkp(0,0));
    FOR(c,0,2*N-2)
    {
        int cx=q.front().f,cy=q.front().s;
        cout<<M[cx][cy];
        int Min=100;
        vector<pii> Nxt;
        while( !q.empty() )
        {
            int cx=q.front().f,cy=q.front().s;
            q.pop();
            if( cx!=N-1 )
            {
                if( M[cx+1][cy]-'a' <= Min )
                {
                    Min=M[cx+1][cy]-'a';
                    Nxt.pb(mkp(cx+1,cy));
                }
            }
            if( cy!=N-1 )
            {
                if( M[cx][cy+1]-'a' <= Min )
                {
                    Min=M[cx][cy+1]-'a';
                    Nxt.pb(mkp(cx,cy+1));
                }
            }
        }
        for(pii Nxi : Nxt)
        {
            if( M[Nxi.f][Nxi.s]-'a'==Min )q.push(mkp(Nxi.f,Nxi.s));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pcc pair<int, char>
#define pci pair<pair<int, char>  ,pii>
string C[2002];
int n , k;
vector<pii> pos[27];
pii pai[2002][2002];
int dist[2002][2002];
bool vis[2002][2002];
int posx[2002];
int32_t main(){
	cin>>n>>k;
	for(int i = 0 ; i < n; i ++)
		for(int j = 0 ; j < n ; j ++) pai[i][j] = pii(-1,-1) , vis[i][j] = 0 , posx[i+j] = 15165165165;
	for(int  i = 0 ; i < n; i ++) cin>>C[i];
	if(k >= 2*n-1){
		for(int i = 0 ; i < 2*n-1 ; i++) cout<<'a';
		cout<<endl;
		return 0;
	}
	else{
		for(int i = 0 ; i < n; i ++){
			for(int j = 0 ; j < n ; j++){
				dist[i][j] = (C[i][j] != 'a');
				if(j && i){
					dist[i][j] += min(dist[i-1][j] , dist[i][j-1]);
				}
				else if(j) dist[i][j] += dist[i][j-1];
				else if(i) dist[i][j] += dist[i-1][j];
			}
		}
		// acha o maior cara com manhattan maior e dist == k + 1;
		if(dist[n-1][n-1] <= k){
			for(int i = 0 ; i < 2*n - 1 ; i++) cout<<'a';
			cout<<endl;
			return 0;
		}
		else{
			int mdist = 0;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j++){
					if(C[i][j] != 'a')
					if(dist[i][j] == k + 1) mdist = max(i+j , mdist);
					else
						if(dist[i][j] == k) mdist = max(i+j , mdist);
				}
			}
			priority_queue< pci , vector<pci> , greater<pci> > pq;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j ++)
					if(dist[i][j] == k + 1 && (i+j) == mdist){
						pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
					}
			}
			while(!pq.empty()){
				pci u = pq.top();
				pq.pop();
				if(vis[u.S.F][u.S.S] || (posx[u.S.F + u.S.S] < (C[u.S.F][u.S.S] - 'a'))) continue;
				vis[u.S.F][u.S.S] = true;
				posx[u.S.F + u.S.S] = C[u.S.F][u.S.S] - 'a';
				if(u.S.F == (n - 1 )&& u.S.S == (n-1)) break;
				if(u.S.F +1 < n  && pai[u.S.F+1][u.S.S] == pii(-1,-1)){
					pci r = pci(pcc( u.S.F + u.S.S + 1 , C[u.S.F + 1][u.S.S]) , pii(u.S.F + 1  , u.S.S));
					pai[r.S.F][r.S.S] = u.S;
					 pq.push(r);
				}
				if((u.S.S + 1) < n && pai[u.S.F][u.S.S+1] == pii(-1,-1)){
					pci r = pci(pcc(u.F.F + 1 , C[u.S.F][u.S.S + 1]) , pii(u.S.F , u.S.S + 1));
					pai[r.S.F][r.S.S] = u.S;
					pq.push(r);
				}
			}
			for(int i = 0 ; i < mdist ; i++){
				cout<<'a';
			}
			vector<char> U;
			pii T = pii(n-1 , n-1);
			while(T.F != -1 && T.S != -1){
				U.push_back(C[T.F][T.S]);
				T = pai[T.F][T.S];
			}
			reverse(U.begin() , U.end());
			for(auto w : U){
				cout<<w;
			}
			cout<<endl;
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pcc pair<int, char>
#define pci pair<pair<int, char>  ,pii>
string C[2002];
int n , k;
vector<pii> pos[27];
pii pai[2002][2002];
int dist[2002][2002];
bool vis[2002][2002];
int posx[4500];
int32_t main(){
	cin>>n>>k;
	for(int i = 0 ; i < n; i ++)
		for(int j = 0 ; j < n ; j ++) pai[i][j] = pii(-1,-1) , vis[i][j] = 0 , posx[i+j] = 15165165165;
	for(int  i = 0 ; i < n; i ++) cin>>C[i];
	if(k >= 2*n-1){
		for(int i = 0 ; i < 2*n-1 ; i++) cout<<'a';
		cout<<endl;
		return 0;
	}
	else{
		for(int i = 0 ; i < n; i ++){
			for(int j = 0 ; j < n ; j++){
				dist[i][j] = (C[i][j] != 'a');
				if(j && i){
					dist[i][j] += min(dist[i-1][j] , dist[i][j-1]);
				}
				else if(j) dist[i][j] += dist[i][j-1];
				else if(i) dist[i][j] += dist[i-1][j];
			}
		}
		// acha o maior cara com manhattan maior e dist == k + 1;
		if(dist[n-1][n-1] <= k){
			for(int i = 0 ; i < 2*n - 1 ; i++) cout<<'a';
			cout<<endl;
			return 0;
		}
		else{
			int mdist = 0;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j++){
					if(C[i][j] != 'a')
					if(dist[i][j] == k + 1) mdist = max(i+j , mdist);
					else
						if(dist[i][j] == k) mdist = max(i+j , mdist);
				}
			}
			priority_queue< pci , vector<pci> , greater<pci> > pq;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j ++)
					if(dist[i][j] == k + 1 && (i+j) == mdist){
						pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
					}
			}
			while(!pq.empty()){
				pci u = pq.top();
				pq.pop();
				if(vis[u.S.F][u.S.S] || (posx[u.S.F + u.S.S] < (C[u.S.F][u.S.S] - 'a'))) continue;
				vis[u.S.F][u.S.S] = true;
				posx[u.S.F + u.S.S] = C[u.S.F][u.S.S] - 'a';
				if(u.S.F == (n - 1 )&& u.S.S == (n-1)) break;
				if(u.S.F +1 < n  && pai[u.S.F+1][u.S.S] == pii(-1,-1)){
					pci r = pci(pcc( u.S.F + u.S.S + 1 , C[u.S.F + 1][u.S.S]) , pii(u.S.F + 1  , u.S.S));
					pai[r.S.F][r.S.S] = u.S;
					 pq.push(r);
				}
				if((u.S.S + 1) < n && pai[u.S.F][u.S.S+1] == pii(-1,-1)){
					pci r = pci(pcc(u.F.F + 1 , C[u.S.F][u.S.S + 1]) , pii(u.S.F , u.S.S + 1));
					pai[r.S.F][r.S.S] = u.S;
					pq.push(r);
				}
			}
			for(int i = 0 ; i < mdist ; i++){
				cout<<'a';
			}
			vector<char> U;
			pii T = pii(n-1 , n-1);
			while(T.F != -1 && T.S != -1){
				U.push_back(C[T.F][T.S]);
				T = pai[T.F][T.S];
			}
			reverse(U.begin() , U.end());
			for(auto w : U){
				cout<<w;
			}
			cout<<endl;
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pcc pair<int, char>
#define pci pair<pair<int, char>  ,pii>
string C[2002];
int n , k;
pii pai[2002][2002];
int dist[2002][2002];
bool vis[2002][2002];
int posx[4500];
int32_t main(){
	cin>>n>>k;
	for(int i = 0 ; i < n; i ++)
		for(int j = 0 ; j < n ; j ++) pai[i][j] = pii(-1,-1) , vis[i][j] = 0 , posx[i+j] = 15165165165;
	for(int  i = 0 ; i < n; i ++) cin>>C[i];
	if(k >= 2*n-1){
		for(int i = 0 ; i < 2*n-1 ; i++) cout<<'a';
		cout<<endl;
		return 0;
	}
	else{
		for(int i = 0 ; i < n; i ++){
			for(int j = 0 ; j < n ; j++){
				dist[i][j] = (C[i][j] != 'a');
				if(j && i){
					dist[i][j] += min(dist[i-1][j] , dist[i][j-1]);
				}
				else if(j) dist[i][j] += dist[i][j-1];
				else if(i) dist[i][j] += dist[i-1][j];
			}
		}
		// acha o maior cara com manhattan maior e dist == k + 1;
		if(dist[n-1][n-1] <= k){
			for(int i = 0 ; i < 2*n - 1 ; i++) cout<<'a';
			cout<<endl;
			return 0;
		}
		else{
			int mdist = 0;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j++){
					if(C[i][j] != 'a'){
					if(dist[i][j] == k + 1) mdist = max(i+j , mdist);
					}
					else
						if(dist[i][j] == k) mdist = max(i+j , mdist);
				}
			}
			priority_queue< pci , vector<pci> , greater<pci> > pq;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j ++){
					if(dist[i][j] == k + 1 && (i+j) == mdist && C[i][j] != 'a'){
						pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
					}
					if(dist[i][j] == k && (i+j) == mdist) pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
				}
			}
			while(!pq.empty()){
				pci u = pq.top();
				pq.pop();
				if(vis[u.S.F][u.S.S] || (posx[u.S.F + u.S.S] < (C[u.S.F][u.S.S] - 'a'))) continue;
				vis[u.S.F][u.S.S] = true;
				posx[u.S.F + u.S.S] = C[u.S.F][u.S.S] - 'a';
				if(u.S.F == (n - 1 )&& u.S.S == (n-1)) break;
				if(u.S.F +1 < n  && pai[u.S.F+1][u.S.S] == pii(-1,-1)){
					pci r = pci(pcc( u.S.F + u.S.S + 1 , C[u.S.F + 1][u.S.S]) , pii(u.S.F + 1  , u.S.S));
					pai[r.S.F][r.S.S] = u.S;
					 pq.push(r);
				}
				if((u.S.S + 1) < n && pai[u.S.F][u.S.S+1] == pii(-1,-1)){
					pci r = pci(pcc(u.F.F + 1 , C[u.S.F][u.S.S + 1]) , pii(u.S.F , u.S.S + 1));
					pai[r.S.F][r.S.S] = u.S;
					pq.push(r);
				}
			}
			for(int i = 0 ; i < mdist ; i++){
				cout<<'a';
			}
			vector<char> U;
			pii T = pii(n-1 , n-1);
			while(T.F != -1 && T.S != -1){
				U.push_back(C[T.F][T.S]);
				T = pai[T.F][T.S];
			}
			reverse(U.begin() , U.end());
			for(auto w : U){
				cout<<w;
			}
			cout<<endl;
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pcc pair<int, char>
#define pci pair<pair<int, char>  ,pii>
string C[2002];
int n , k;
pii pai[2002][2002];
int dist[2002][2002];
bool vis[2002][2002];
int posx[4500];
int32_t main(){
	cin>>n>>k;
	for(int i = 0 ; i < n; i ++)
		for(int j = 0 ; j < n ; j ++) pai[i][j] = pii(-1,-1) , vis[i][j] = 0 , posx[i+j] = 15165165165;
	for(int  i = 0 ; i < n; i ++) cin>>C[i];
	if(k >= 2*n-1){
		for(int i = 0 ; i < 2*n-1 ; i++) cout<<'a';
		cout<<endl;
		return 0;
	}
	else{
		for(int i = 0 ; i < n; i ++){
			for(int j = 0 ; j < n ; j++){
				dist[i][j] = (C[i][j] != 'a');
				if(j && i){
					dist[i][j] += min(dist[i-1][j] , dist[i][j-1]);
				}
				else if(j) dist[i][j] += dist[i][j-1];
				else if(i) dist[i][j] += dist[i-1][j];
			}
		}
		// acha o maior cara com manhattan maior e dist == k + 1;
		if(dist[n-1][n-1] <= k){
			for(int i = 0 ; i < 2*n - 1 ; i++) cout<<'a';
			cout<<endl;
			return 0;
		}
		else{
			int mdist = 0;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j++){
					if(C[i][j] != 'a'){
					if(dist[i][j] == k + 1) mdist = max(i+j , mdist);
					}
					else
						if(dist[i][j] == k) mdist = max(i+j , mdist);
				}
			}
			priority_queue< pci , vector<pci> , greater<pci> > pq;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j ++)
					if(dist[i][j] == k + 1 && (i+j) == mdist && C[i][j] != 'a'){
						pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
					}
					if(dist[i][j] == k && (i+j) == mdist)pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
			}
			while(!pq.empty()){
				pci u = pq.top();
				pq.pop();
				if(vis[u.S.F][u.S.S] || (posx[u.S.F + u.S.S] < (C[u.S.F][u.S.S] - 'a'))) continue;
				vis[u.S.F][u.S.S] = true;
				posx[u.S.F + u.S.S] = C[u.S.F][u.S.S] - 'a';
				if(u.S.F == (n - 1 )&& u.S.S == (n-1)) break;
				if(u.S.F +1 < n  && pai[u.S.F+1][u.S.S] == pii(-1,-1)){
					pci r = pci(pcc( u.S.F + u.S.S + 1 , C[u.S.F + 1][u.S.S]) , pii(u.S.F + 1  , u.S.S));
					pai[r.S.F][r.S.S] = u.S;
					 pq.push(r);
				}
				if((u.S.S + 1) < n && pai[u.S.F][u.S.S+1] == pii(-1,-1)){
					pci r = pci(pcc(u.F.F + 1 , C[u.S.F][u.S.S + 1]) , pii(u.S.F , u.S.S + 1));
					pai[r.S.F][r.S.S] = u.S;
					pq.push(r);
				}
			}
			for(int i = 0 ; i < mdist ; i++){
				cout<<'a';
			}
			vector<char> U;
			pii T = pii(n-1 , n-1);
			while(T.F != -1 && T.S != -1){
				U.push_back(C[T.F][T.S]);
				T = pai[T.F][T.S];
			}
			reverse(U.begin() , U.end());
			for(auto w : U){
				cout<<w;
			}
			cout<<endl;
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pcc pair<int, char>
#define pci pair<pair<int, char>  ,pii>
string C[2002];
int n , k;
pii pai[2002][2002];
int dist[2002][2002];
bool vis[2002][2002];
int posx[4500];
int32_t main(){
	cin>>n>>k;
	for(int i = 0 ; i < n; i ++)
		for(int j = 0 ; j < n ; j ++) pai[i][j] = pii(-1,-1) , vis[i][j] = 0 , posx[i+j] = 15165165165;
	for(int  i = 0 ; i < n; i ++) cin>>C[i];
	if(k >= 2*n-1){
		for(int i = 0 ; i < 2*n-1 ; i++) cout<<'a';
		cout<<endl;
		return 0;
	}
	else{
		for(int i = 0 ; i < n; i ++){
			for(int j = 0 ; j < n ; j++){
				dist[i][j] = (C[i][j] != 'a');
				if(j && i){
					dist[i][j] += min(dist[i-1][j] , dist[i][j-1]);
				}
				else if(j) dist[i][j] += dist[i][j-1];
				else if(i) dist[i][j] += dist[i-1][j];
			}
		}
		// acha o maior cara com manhattan maior e dist == k + 1;
		if(dist[n-1][n-1] <= k){
			for(int i = 0 ; i < 2*n - 1 ; i++) cout<<'a';
			cout<<endl;
			return 0;
		}
		else{
			int mdist = 0;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j++){
					if(C[i][j] != 'a'){
					if(dist[i][j] == k + 1) mdist = max(i+j , mdist);
					}
					else
						if(dist[i][j] == k) mdist = max(i+j , mdist);
				}
			}
			priority_queue< pci , vector<pci> , greater<pci> > pq;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j ++)
					if(dist[i][j] == k + 1 && (i+j) == mdist){
						pq.push(pci(pcc(i+j, C[i][j]), pii(i,j)));
					}
			}
			while(!pq.empty()){
				pci u = pq.top();
				pq.pop();
				if(vis[u.S.F][u.S.S] || (posx[u.S.F + u.S.S] < (C[u.S.F][u.S.S] - 'a'))) continue;
				vis[u.S.F][u.S.S] = true;
				posx[u.S.F + u.S.S] = C[u.S.F][u.S.S] - 'a';
				if(u.S.F == (n - 1 )&& u.S.S == (n-1)) break;
				if(u.S.F +1 < n  && pai[u.S.F+1][u.S.S] == pii(-1,-1)){
					pci r = pci(pcc( u.S.F + u.S.S + 1 , C[u.S.F + 1][u.S.S]) , pii(u.S.F + 1  , u.S.S));
					pai[r.S.F][r.S.S] = u.S;
					 pq.push(r);
				}
				if((u.S.S + 1) < n && pai[u.S.F][u.S.S+1] == pii(-1,-1)){
					pci r = pci(pcc(u.F.F + 1 , C[u.S.F][u.S.S + 1]) , pii(u.S.F , u.S.S + 1));
					pai[r.S.F][r.S.S] = u.S;
					pq.push(r);
				}
			}
			for(int i = 0 ; i < mdist ; i++){
				cout<<'a';
			}
			vector<char> U;
			pii T = pii(n-1 , n-1);
			while(T.F != -1 && T.S != -1){
				U.push_back(C[T.F][T.S]);
				T = pai[T.F][T.S];
			}
			reverse(U.begin() , U.end());
			for(auto w : U){
				cout<<w;
			}
			cout<<endl;
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pci pair<char ,pii>
string C[2002];
int n , k;
vector<pii> pos[27];
pii pai[2002][2002];
int dist[2002][2002];
bool vis[2002][2002];
int posx[4005];
int32_t main(){
	cin>>n>>k;
	for(int i = 0 ; i < n; i ++)
		for(int j = 0 ; j < n ; j ++) pai[i][j] = pii(-1,-1) , vis[i][j] = 0 ,posx[i+j] = 514161;
	for(int  i = 0 ; i < n; i ++) cin>>C[i];
	if(k >= 2*n-1){
		for(int i = 0 ; i < 2*n-1 ; i++) cout<<'a';
		cout<<endl;
		return 0;
	}
	else{
		for(int i = 0 ; i < n; i ++){
			for(int j = 0 ; j < n ; j++){
				dist[i][j] = (C[i][j] != 'a');
				if(j && i){
					dist[i][j] += min(dist[i-1][j] , dist[i][j-1]);
				}
				else if(j) dist[i][j] += dist[i][j-1];
				else if(i) dist[i][j] += dist[i-1][j];
			}
		}
		// acha o maior cara com manhattan maior e dist == k + 1;
		if(dist[n-1][n-1] <= k){
			for(int i = 0 ; i < 2*n - 1 ; i++) cout<<'a';
			cout<<endl;
			return 0;
		}
		else{
			int mdist = 0;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j++){
					bool R = (C[i][j] == 'a');
					if(dist[i][j] == k + 1  - R) mdist = max(i+j , mdist);
				}
			}
			priority_queue< pci , vector<pci> , greater<pci> > pq;
			for(int i = 0 ; i < n; i ++){
				for(int j = 0 ; j < n ; j ++)
					if(dist[i][j] == k + 1 && (i+j) == mdist){
						pq.push(pci(C[i][j] , pii(i,j)));
					}
			}
			while(!pq.empty()){
				pci u = pq.top();
				pq.pop();
				if(vis[u.S.F][u.S.S] || (u.F - 'a') > posx[u.S.S + u.S.F]) continue;
				vis[u.S.F][u.S.S] = true;
				posx[u.S.S + u.S.F] = u.F - 'a';
				//cout<<u.S.F <<" " <<u.S.S << endl;
				if(u.S.F == (n - 1 )&& u.S.S == (n-1)) break;
				if(u.S.F +1 < n  && !vis[u.S.F + 1][u.S.S]){

					pci r = pci(C[u.S.F + 1][u.S.S] , pii(u.S.F + 1  , u.S.S));
					pai[r.S.F][r.S.S] = u.S;
					if(!vis[r.S.F][r.S.S]) pq.push(r);
				}
				if((u.S.S + 1) < n && !vis[u.S.F][u.S.S + 1]){
					pci r = pci(C[u.S.F][u.S.S + 1] , pii(u.S.F , u.S.S + 1));
					pai[r.S.F][r.S.S] = u.S;
					pq.push(r);
				}
			}
			for(int i = 0 ; i < mdist ; i++){
				cout<<'a';
			}
			vector<char> U;
			pii T = pii(n-1 , n-1);
			while(T.F != -1 && T.S != -1){
				U.push_back(C[T.F][T.S]);
				T = pai[T.F][T.S];
			}
			reverse(U.begin() , U.end());
			for(auto w : U){
				cout<<w;
			}
			cout<<endl;
			return 0;
		}
	}
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define start_routine clock_t begin = clock();
#define end_routine clock_t end = clock(); double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;return cerr<<"\nTime elapsed : "<<elapsed_secs<<" seconds\n",0;
#define ll          long long
#define pb          push_back
#define endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[2005][2005],recent[2005][2005];
string s[2002];
void go(int m){
    if(m==2*n-2) return;
    char c='z';
    for(int i=0;i<n;i++){
        int j= m-i;
        if(j<0 || j>=n || recent[i][j]==0) continue;
        if(j+1<n) c=min(c,s[i][j+1]);
        if(i+1<n) c=min(c,s[i+1][j]);
    }
    for(int i=0;i<n;i++){
        int j=m-i;
        if(j<0 || j>=n || recent[i][j]==0) continue;
        if(j+1<n && s[i][j+1]==c) recent[i][j+1]=1;
        if(i+1<n && s[i+1][j]==c) recent[i+1][j]=1;
    }
    cout<<c;
    go(m+1);
}
int main()
{
    ios
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>s[i];
    dp[0][0]=(s[0][0]!='a');
    for(int i=1;i<n;i++){
        dp[i][0] = dp[i-1][0]+(s[i][0]!='a');
        dp[0][i] = dp[0][i-1]+(s[0][i]!='a');
    }
    int A = -1;
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            dp[i][j] = min(dp[i-1][j],dp[i][j-1])+(s[i][j]!='a');
            if(dp[i][j]<=k) A=max(A,i+j);
        }
    }
    if(A==-1){
        cout<<s[0][0];
        recent[0][0]=1;
        go(0);
    }
    else{
        for(int i=0;i<=A;i++) cout<<'a';
        for(int i=0;i<n;i++){
            int j= A-i;
            if(j>=0 && j<n && dp[i][j]<=k) recent[i][j]=1;
        }
        go(A);
    }
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define start_routine clock_t begin = clock();
#define end_routine clock_t end = clock(); double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;return cerr<<"\nTime elapsed : "<<elapsed_secs<<" seconds\n",0;
#define ll          long long
#define pb          push_back
#define endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[2005][2005],recent[2005][2005];
string s[2002];
void go(int m){
    if(m==2*n-2) return;
    char c='z';
    for(int i=0;i<n;i++){
        int j= m-i;
        if(j+1<n && recent[i][j]) c=min(c,s[i][j+1]);
        if(i+1<n && recent[i][j]) c=min(c,s[i+1][j]);
    }
    for(int i=0;i<n;i++){
        int j=m-i;
        if(j+1<n && s[i][j+1]==c) recent[i][j+1]=1;
        if(i+1<n && s[i+1][j]==c) recent[i+1][j]=1;
    }
    cout<<c;
    go(m+1);
}
int main()
{
    ios
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>s[i];
    dp[0][0]=(s[0][0]!='a');
    for(int i=1;i<n;i++){
        dp[i][0] = dp[i-1][0]+(s[i][0]!='a');
        dp[0][i] = dp[0][i-1]+(s[0][i]!='a');
    }
    int A = -1;
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            dp[i][j] = min(dp[i-1][j],dp[i][j-1])+(s[i][j]!='a');
            if(dp[i][j]<=k) A=max(A,i+j);
        }
    }
    if(k==0){
        cout<<s[0][0];
        recent[0][0]=1;
        go(0);
    }
    else{
        for(int i=0;i<=A;i++) cout<<'a';
        for(int i=0;i<n;i++){
            int j= A-i;
            if(j>=0 && j<n) recent[i][j]=1;
        }
        go(A);
    }
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define start_routine clock_t begin = clock();
#define end_routine clock_t end = clock(); double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;return cerr<<"\nTime elapsed : "<<elapsed_secs<<" seconds\n",0;
#define ll          long long
#define pb          push_back
#define endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[2005][2005],recent[2005][2005];
string s[2002];
void go(int m){
    if(m==2*n-2) return;
    char c='z';
    for(int i=0;i<n;i++){
        int j= m-i;
        if(j<0 || j>=n || recent[i][j]==0) continue;
        if(j+1<n) c=min(c,s[i][j+1]);
        if(i+1<n) c=min(c,s[i+1][j]);
    }
    for(int i=0;i<n;i++){
        int j=m-i;
        if(j<0 || j>=n || recent[i][j]==0) continue;
        if(j+1<n && s[i][j+1]==c) recent[i][j+1]=1;
        if(i+1<n && s[i+1][j]==c) recent[i+1][j]=1;
    }
    cout<<c;
    go(m+1);
}
int main()
{
    ios
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>s[i];
    dp[0][0]=(s[0][0]!='a');
    for(int i=1;i<n;i++){
        dp[i][0] = dp[i-1][0]+(s[i][0]!='a');
        dp[0][i] = dp[0][i-1]+(s[0][i]!='a');
    }
    int A = -1;
    //cout<<A<<endl;
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            dp[i][j] = min(dp[i-1][j],dp[i][j-1])+(s[i][j]!='a');
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(dp[i][j]<=k) A=max(A,i+j);
        }

    }
    if(A==-1){
        cout<<s[0][0];
        recent[0][0]=1;
        go(0);
    }
    else{
        for(int i=0;i<=A;i++) cout<<'a';
        for(int i=0;i<n;i++){
            int j= A-i;
            if(j>=0 && j<n && dp[i][j]<=k) recent[i][j]=1;
        }
        go(A);
    }
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define start_routine clock_t begin = clock();
#define end_routine clock_t end = clock(); double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;return cerr<<"\nTime elapsed : "<<elapsed_secs<<" seconds\n",0;
#define ll          long long
#define pb          push_back
#define endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[2005][2005],recent[2005][2005];
string s[2002];
void go(int m){
    if(m==2*n-2) return;
    char c='z';
    for(int i=0;i<n;i++){
        int j= m-i;
        if(j+1<n && j+1>=0 && recent[i][j]) c=min(c,s[i][j+1]);
        if(i+1<n && j+1>=0 &&recent[i][j]) c=min(c,s[i+1][j]);
    }
    for(int i=0;i<n;i++){
        int j=m-i;
        if(j+1<n && j+1>=0 && s[i][j+1]==c) recent[i][j+1]=1;
        if(i+1<n && j+1>=0 &&s[i+1][j]==c) recent[i+1][j]=1;
    }
    cout<<c;
    go(m+1);
}
int main()
{
    ios
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>s[i];
    dp[0][0]=(s[0][0]!='a');
    for(int i=1;i<n;i++){
        dp[i][0] = dp[i-1][0]+(s[i][0]!='a');
        dp[0][i] = dp[0][i-1]+(s[0][i]!='a');
    }
    int A = -1;
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            dp[i][j] = min(dp[i-1][j],dp[i][j-1])+(s[i][j]!='a');
            if(dp[i][j]<=k) A=max(A,i+j);
        }
    }
    if(k==0){
        cout<<s[0][0];
        recent[0][0]=1;
        go(0);
    }
    else{
        for(int i=0;i<=A;i++) cout<<'a';
        for(int i=0;i<n;i++){
            int j= A-i;
            if(j>=0 && j<n) recent[i][j]=1;
        }
        go(A);
    }
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define start_routine clock_t begin = clock();
#define end_routine clock_t end = clock(); double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;return cerr<<"\nTime elapsed : "<<elapsed_secs<<" seconds\n",0;
#define ll          long long
#define pb          push_back
#define endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[2005][2005],recent[2005][2005];
string s[2002];
void go(int m){
    if(m==2*n-2) return;
    char c='z';
    for(int i=0;i<n;i++){
        int j= m-i;
        if(j<0 || j>=n || recent[i][j]==0) continue;
        if(j+1<n) c=min(c,s[i][j+1]);
        if(i+1<n) c=min(c,s[i+1][j]);
    }
    for(int i=0;i<n;i++){
        int j=m-i;
        if(j<0 || j>=n || recent[i][j]==0) continue;
        if(j+1<n && s[i][j+1]==c) recent[i][j+1]=1;
        if(i+1<n && s[i+1][j]==c) recent[i+1][j]=1;
    }
    cout<<c;
    go(m+1);
}
int main()
{
    ios
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>s[i];
    dp[0][0]=(s[0][0]!='a');
    for(int i=1;i<n;i++){
        dp[i][0] = dp[i-1][0]+(s[i][0]!='a');
        dp[0][i] = dp[0][i-1]+(s[0][i]!='a');
    }
    int A = -1;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(dp[i][j]<=k) A=max(A,i+j);
            if(i==0 || j==0) continue;
            dp[i][j] = min(dp[i-1][j],dp[i][j-1])+(s[i][j]!='a');
        }
    }
    if(A==-1){
        cout<<s[0][0];
        recent[0][0]=1;
        go(0);
    }
    else{
        for(int i=0;i<=A;i++) cout<<'a';
        for(int i=0;i<n;i++){
            int j= A-i;
            if(j>=0 && j<n && dp[i][j]<=k) recent[i][j]=1;
        }
        go(A);
    }
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define start_routine clock_t begin = clock();
#define end_routine clock_t end = clock(); double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;return cerr<<"\nTime elapsed : "<<elapsed_secs<<" seconds\n",0;
#define ll          long long
#define pb          push_back
#define endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[2005][2005],recent[2005][2005];
string s[2002];
void go(int m){
    if(m==2*n-2) return;
    char c='z';
    for(int i=0;i<n;i++){
        int j= m-i;
        if(j+1<n && j+1>=0 && recent[i][j]) c=min(c,s[i][j+1]);
        if(i+1<n && recent[i][j]) c=min(c,s[i+1][j]);
    }
    for(int i=0;i<n;i++){
        int j=m-i;
        if(recent[i][j]==0) continue;
        if(j+1<n && j+1>=0 && s[i][j+1]==c ) recent[i][j+1]=1;
        if(i+1<n && j+1>=0 &&s[i+1][j]==c) recent[i+1][j]=1;
    }
    cout<<c;
    go(m+1);
}
int main()
{
    ios
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>s[i];
    dp[0][0]=(s[0][0]!='a');
    for(int i=1;i<n;i++){
        dp[i][0] = dp[i-1][0]+(s[i][0]!='a');
        dp[0][i] = dp[0][i-1]+(s[0][i]!='a');
    }
    int A = -1;
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            dp[i][j] = min(dp[i-1][j],dp[i][j-1])+(s[i][j]!='a');
            if(dp[i][j]<=k) A=max(A,i+j);
        }
    }
    if(k==0){
        cout<<s[0][0];
        recent[0][0]=1;
        go(0);
    }
    else{
        for(int i=0;i<=A;i++) cout<<'a';
        for(int i=0;i<n;i++){
            int j= A-i;
            if(j>=0 && j<n && dp[i][j]<=k) recent[i][j]=1;
        }
        go(A);
    }
}