#include <bits/stdc++.h>   using namespace std;   FILE *in;   FILE *out;   int n, m, k;   char a[64][64];   int blocks[64][64];   int cell[64 * 64][2];   int dyn[64 * 64][4][2];   int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};   void fill(int row, int col, int color) {     blocks[row][col] = color;     for (int i = 0; i < 4; i++) {       int nrow = row + dir[i][0];       if (nrow < 0 || nrow >= n) continue;       int ncol = col + dir[i][1];       if (ncol < 0 || ncol >= m) continue;       if (a[row][col] == a[nrow][ncol] && blocks[nrow][ncol] == -1)         fill(nrow, ncol, color);     }   }   void getBlocks() {     int nextColor = 0;     memset(blocks, -1, sizeof(blocks));     for (int i = 0; i < n; i++) {       for (int c = 0; c < m; c++) {         if (a[i][c] !=  0  && blocks[i][c] == -1) {           fill(i, c, nextColor);           cell[nextColor][0] = i;           cell[nextColor][1] = c;           nextColor++;         }       }     }   }   int main(void) {     in = stdin;     out = stdout;     fscanf(in,  %d %d , &n, &k);     for (int i = 0; i < n; i++) fscanf(in,  %s , a[i]);     m = (int)strlen(a[0]);     getBlocks();     memset(dyn, -1, sizeof(dyn));     int rem = k, cur = 0;     int bp = 0, dp = 1, cp = 0;     int foundCycle = 0;     while (rem) {       rem--;       cur++;       if (!foundCycle && dyn[bp][dp][cp] != -1) {         foundCycle = 1;         rem %= (cur - dyn[bp][dp][cp]);       }       dyn[bp][dp][cp] = cur;       int row = cell[bp][0], col = cell[bp][1];       while (1) {         int nrow = row + dir[dp][0];         if (nrow < 0 || nrow >= n) break;         int ncol = col + dir[dp][1];         if (ncol < 0 || ncol >= m) break;         if (a[nrow][ncol] != a[row][col]) break;         row = nrow;         col = ncol;       }       int dd = (cp ? ((dp + 1) % 4) : ((dp + 3) % 4));       while (1) {         int nrow = row + dir[dd][0];         if (nrow < 0 || nrow >= n) break;         int ncol = col + dir[dd][1];         if (ncol < 0 || ncol >= m) break;         if (a[nrow][ncol] != a[row][col]) break;         row = nrow;         col = ncol;       }       int nrow = row + dir[dp][0];       int ncol = col + dir[dp][1];       if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < m &&           a[nrow][ncol] !=  0 ) {         bp = blocks[nrow][ncol];         continue;       }       if (cp == 0)         cp = 1;       else {         cp = 0;         dp = (dp + 1) % 4;       }     }     fprintf(out,  %c n , a[cell[bp][0]][cell[bp][1]]);     return 0;   }   