#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
vector<string> x;

void en(int state, int &dx, int &dy) {
    if(state / 2 % 2 == 0) {
        if(state % 2 == 0) {
            dx = 0;
        } else {
            dy = 0;
        }
    } else {
        if(state % 2 != 0) {
            dx = 0;
        } else {
            dy = 0;
        }
    }
}

void par(int state, int &dx, int &dy) {
    dx = -1;
    dy = -1;
    if(state / 2 == 0) {
        dx = 1;
    } else if(state / 2 == 1) {
        dx = 1;
        dy = 1;
    } else if(state / 2 == 2) {
        dy = 1;
    }
}

void step(pair<int, int> &k, int &state) {
    int dx, dy;
    par(state, dx, dy);
    while(x[k.first][k.second + dx] == x[k.first][k.second]) {
        k.second += dx;
    }
    while(x[k.first + dy][k.second] == x[k.first][k.second]) {
        k.first += dy;
    }
    en(state, dx, dy);

    if(x[k.first + dy][k.second + dx] == '0'){
        state++;
        state %= 8;
    }else{
        k.first += dy;
        k.second += dx;
    }
    //cout << state << ' ' << k.first << ' ' << k.second << '\n';
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    x = vector<string>(m + 2, "0");
    pair<int, int> bp(1, 1);
    //int dp = 2;
    int state = 1;
    //bool cp = false;
    getchar();
    for(int i = 1; i <= m; i++) {
        char c = getchar();
        while(c != '\n' && c != EOF) {
            x[i] += c;
            c = getchar();
        }
        x[i] += '0';
    }
    for(int i = 1; i < x[1].size(); i++) {
        x[0] += '0';
        x[m + 1] += '0';
    }
    for(int i = 0; i < n; i++) {
        /*if(dp % 2 == 0) {
            while(x[bp.first][bp.second + dp - 1] == x[bp.first][bp.second]) {
                bp.second += dp - 1;
            }

            if(x[bp.first][bp.second + dp - 1] == '0') {
                if(cp) {
                    dp++;
                    dp %= 4;
                }
                cp = !cp;
            } else {
                bp.second += dp - 1;
            }
        } else {
            while(x[bp.first + dp - 2][bp.second] == x[bp.first][bp.second]) {
                bp.first += dp - 2;
            }
            if(x[bp.first + dp - 2][bp.second] == '0') {
                if(cp) {
                    dp++;
                    dp %= 4;
                }
                cp = !cp;
            } else {
                bp.first += dp - 2;
            }
        }*/
        step(bp, state);
    }
    printf("%d", x[bp.first], bp.second);
    return 0;
}
