import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class D {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static PrintWriter writer;

    static void init() {
        reader = new BufferedReader(new InputStreamReader(System.in));

        writer = new PrintWriter(System.out);
    }

    static String nextToken() {
        while (tokenizer == null || !tokenizer.hasMoreTokens())
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                System.out.println(e);
                System.exit(1);
            }
        return tokenizer.nextToken();
    }

    static byte nextByte() {
        return Byte.parseByte(nextToken());
    }

    static int nextInt() {
        return Integer.parseInt(nextToken());
    }

    static double nextDouble() {
        return Double.parseDouble(nextToken());
    }

    static void solve() {
        int m = nextInt(), n = nextInt();
        char[][] program = new char[m][];
        for (int i = 0; i < m; i++)
            program[i] = nextToken().toCharArray();
        int l = program[0].length;

        Block[][] blockMap = new Block[m][l];
        ArrayList<Block> blockList = new ArrayList<Block>();
        for (int y = 0; y < m; y++)
            for (int x = 0; x < l; x++)
                if (program[y][x] != '0') {
                    int w = 0, h = 0;
                    while (x + w < l && program[y][x + w] == program[y][x]) w++;
                    while (y + h < m && program[y + h][x] == program[y][x]) h++;
                    Block block = new Block(program[y][x], x, y, x + w - 1, y + h - 1);
                    blockList.add(block);
                    for (int j = y; j < y + h; j++)
                        for (int i = x; i < x + w; i++)
                            blockMap[j][i] = block;
                }

        for (Block block : blockList) {
            if (block.x2 + 1 < l) block.directions[0] = blockMap[block.y1][block.x2 + 1];
            if (block.x2 + 1 < l) block.directions[1] = blockMap[block.y2][block.x2 + 1];
            if (block.y2 + 1 < m) block.directions[2] = blockMap[block.y2 + 1][block.x2];
            if (block.y2 + 1 < m) block.directions[3] = blockMap[block.y2 + 1][block.x1];
            if (block.x1 - 1 >= 0) block.directions[4] = blockMap[block.y2][block.x1 - 1];
            if (block.x1 - 1 >= 0) block.directions[5] = blockMap[block.y1][block.x1 - 1];
            if (block.y1 - 1 >= 0) block.directions[6] = blockMap[block.y1 - 1][block.x1];
            if (block.y1 - 1 >= 0) block.directions[7] = blockMap[block.y1 - 1][block.x2];
        }

        Block block = blockMap[0][0];
        int direction = 0;
        while (n > 0) {
            n--;
            if (block.directions[direction] != null) block = block.directions[direction];
            else direction = (direction + 1) % 8;
        }

        writer.println(block.color);
    }


    static void close() {
        try {
            reader.close();

            writer.flush();
            writer.close();
        } catch (IOException e) {
            System.out.println(e);
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        init();
        solve();
        close();
    }

    static class Block {
        char color;
        int x1, y1, x2, y2;
        Block[] directions;

        Block(char color, int x1, int y1, int x2, int y2) {
            this.color = color;
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            directions = new Block[8];
        }
    }
}
