class Piet:
    inc = [{'x':0,'y':-1},{'x':1,'y':0},{'x':0,'y':1},{'x':-1,'y':0}]
    def __init__(self):
        self.BP = {'x':0,'y':0}
        self.DP = 1
        self.CP = 0
        self.getdata()
        self.go()
    def getdata(self):
        in_line = raw_input().split()
        self.m = int(in_line[0])
        self.n = int(in_line[1])
        self.pixels = []
        for i in range(self.m):
            self.pixels.append(raw_input())
    def is_out_limit(self,x,y):
        if x >= len(self.pixels[0]) or y >= self.m or x<0 or y<0:
            return True
        else:
            return False
    def go(self):
        ans = []
        info = []
        for t in range(self.n):
            while True:
                if self.is_out_limit(self.BP['x']+Piet.inc[self.DP]['x'],self.BP['y']+Piet.inc[self.DP]['y']):
                    break
                curr_color = self.pixels[self.BP['y']][self.BP['x']]
                new_color = self.pixels[self.BP['y']+Piet.inc[self.DP]['y']][self.BP['x']+Piet.inc[self.DP]['x']]
                if curr_color == new_color:
                    self.BP['x'] += Piet.inc[self.DP]['x']
                    self.BP['y'] += Piet.inc[self.DP]['y']
                else:
                    break
            while True:
                if self.is_out_limit(self.BP['x']+Piet.inc[self.CP]['x'],self.BP['y']+Piet.inc[self.CP]['y']):
                    break
                curr_color = self.pixels[self.BP['y']][self.BP['x']]
                new_color = self.pixels[self.BP['y']+Piet.inc[self.CP]['y']][self.BP['x']+Piet.inc[self.CP]['x']]
                if curr_color == new_color:
                    self.BP['x'] += Piet.inc[self.CP]['x']
                    self.BP['y'] += Piet.inc[self.CP]['y']
                else:
                    break
            if  self.is_out_limit(self.BP['x']+Piet.inc[self.DP]['x'],self.BP['y']+Piet.inc[self.DP]['y']) or self.pixels[self.BP['y']+Piet.inc[self.DP]['y']][self.BP['x']+Piet.inc[self.DP]['x']] == '0':
                if self.DP == (self.CP + 1)%4:
                    self.CP = (self.CP + 2)%4
                else:
                    self.DP = (self.DP + 1)%4
                    self.CP = (self.DP - 1)%4
            else:
                self.BP['x'] += Piet.inc[self.DP]['x']
                self.BP['y'] += Piet.inc[self.DP]['y']
            #print(self.BP['x'],self.BP['y'],self.DP,self.CP)
            if [self.BP['x'],self.BP['y'],self.DP,self.CP] in info:
                dot = info.index([self.BP['x'],self.BP['y'],self.DP,self.CP])
                print ans[dot+(self.n-dot)%(len(ans)-dot)]
                break
            else:
                ans.append(self.pixels[self.BP['y']][self.BP['x']])
                info.append([self.BP['x'],self.BP['y'],self.DP,self.CP])
        else:
            print ans[-1]

def main():
    p = Piet()

if __name__ == '__main__':
    main()
