malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T S(T a) {     return a * a;   }   template <class T>   inline string tostring(T a) {     ostringstream os(  );     os << a;     return os.str();   }   template <typename T>   inline long long tolong(T a) {     long long res;     istringstream os(a);     os >> res;     return res;   }   template <typename T>   inline T gcd(T a, T b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   template <typename T>   inline T bigmod(T a, T b, T m) {     if (b == 0)       return 1;     else if (b % 2 == 0)       return S(bigmod(a, b / 2, m)) % m;     else       return (a % m * bigmod(a, b - 1, m)) % m;   }   const int inf = (int)1e9 + 5;   const long long linf = (long long)1e16 + 5;   const long long modd = (long long)1e9 + 7;   const int mod = 10000007;   void pr(int f) {     if (f == 1)       cout <<  YES n ;     else       cout <<  NO n ;   }   bool isprime(long long n) {     if (n < 2) return 0;     if (n == 2)       return 1;     else {       for (int i = 2; i * i <= n; i++)         if (n % i == 0) return 0;     }     return 1;   }   map<int, int> mp;   multiset<string>::iterator it;   deque<char> q;   const int nax = 10e6 + 5;   int t[nax], l[100005], d[100005];   int main() {     ios_base::sync_with_stdio(0);     long long int n, m;     cin >> n >> m;     long long int low, high;     high = ((n - (m - 1)) * (n - m)) / 2;     long long int quo = n / m;     long long int rem = n % m;     low = rem * ((quo + 1) * (quo)) / 2 + (m - rem) * (quo * (quo - 1)) / 2;     cout << low <<     << high;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     bool zero = false;     int *arr = new int[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     int neg = 0;     for (int i = 0; i < n; i++) {       if (arr[i] < 0) neg++;       if (arr[i] == 0) {         zero = true;       }     }     for (int i = 0; k > 0 && i < neg;) {       arr[i] = arr[i] * -1;       k--;       i++;     }     long long int ans = 0;     for (int i = 0; i < n; i++) {       ans += arr[i];     }     int min = INT_MAX;     for (int i = 0; i < n; i++) {       if (arr[i] < min) {         min = arr[i];       }     }     if (k % 2 == 0) {       cout << ans;     } else {       cout << ans - 2 * min;     }     return 0;   }
#include<bits/stdc++.h>   #define fo(i, n) for(int i=0; i<n; i++)   #define boost ios::sync_with_stdio(false); cin.tie(0)   #define ll long long int   using namespace std;      int main()   {   boost;       int t; cin>>t;       while(t--){           int n, max_freq = 0, i; cin>>n;           map<int, int> m;           for(i=0; i<n; i++){               int k; cin>>k;               max_freq = max(max_freq, ++m[k]);           }           cout<<max_freq<<  n ;       }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500001;   int arr[N];   vector<pair<int, int> > v[N];   int ans[N], n, m;   int bit[N];   map<int, int> last;   void update(int idx, int val) {     for (; idx <= n; idx += idx & -idx) {       bit[idx] = min(bit[idx], val);     }   }   int query(int idx) {     int res = n + 1;     for (; idx; idx -= idx & -idx) {       res = min(res, bit[idx]);     }     return res;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       scanf( %d , &arr[i]);       bit[i] = n + 1;     }     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d %d , &x, &y);       v[y].push_back({x, i});     }     for (int i = 1; i <= n; i++) {       if (last.find(arr[i]) != last.end())         update(n - last[arr[i]] + 1, i - last[arr[i]]);       last[arr[i]] = i;       int x = v[i].size();       for (int j = 0; j < x; ++j) {         ans[v[i][j].second] = query(n - v[i][j].first + 1);       }     }     for (int i = 1; i <= m; i++) {       printf( %d n , (ans[i] == n + 1) ? -1 : ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     long long int n, i, t, k, c, s = 0, x, sum = 0, st1 = 0, st2 = 0, curr = 0;     cin >> n;     vector<pair<long long int, long long int> > v;     vector<long long int> p;     for (i = 0; i < n; i++) {       cin >> k >> c;       v.push_back(make_pair(c, k));     }     cin >> t;     for (i = 0; i < t; i++) {       cin >> x;       s = x;       p.push_back(s);     }     p.push_back(100000000000000);     sort(v.begin(), v.end());     while (st1 < n) {       if (curr + v[st1].second < p[st2]) {         sum += (st2 + 1) * (v[st1].first) * (v[st1].second);         curr += v[st1].second;         st1++;       } else {         long long int rem = p[st2] - curr;         curr += rem;         sum += (rem) * (st2 + 1) * (v[st1].first);         v[st1].second -= rem;         st2++;         if (!v[st1].second) {           st1++;         }       }     }     cout << sum;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, m;   int nt, kt, mt;   int su[100004];   long long sum = 0;   double res;   int main() {     scanf( %d%d%d , &n, &k, &m);     nt = n;     kt = k;     mt = m;     for (int i = 0; i < n; i++) {       scanf( %d , su + i);       sum += su[i];     }     res = 1.0 * sum / n;     sort(su, su + n);     long long ts = 0;     for (int i = 1; i <= n; i++) {       ts += su[n - i];       long long rm = m - n + i;       long long te = i;       long long tk = k;       if (rm < 0) {         continue;       }       res = max(res, (ts + min(rm, te * tk)) * 1.0 / te);     }     printf( %lf n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 100;   const int MOD = 1e9 + 7;   const int oo = 1e9 + 100;   const long double EPS = 1e-9;   const string DIRS =  UDLR ;   int n, x, y;   char str[MAXN];   int acc[256][MAXN];   int get(char ch, int fr, int to) {     if (to < fr) return 0;     return acc[ch][to] - acc[ch][fr - 1];   }   int main() {     while (scanf( %d %s %d %d , &n, str + 1, &x, &y) == 4) {       for (auto& e : DIRS) {         acc[e][0] = 0;         for (int i = 1; i <= n; ++i) {           acc[e][i] = acc[e][i - 1] + (str[i] == e);         }       }       int init_x = acc[ R ][n] - acc[ L ][n];       int init_y = acc[ U ][n] - acc[ D ][n];       if (init_x == x && init_y == y) {         puts( 0 );         continue;       }       if (abs(x) + abs(y) > n || (n - abs(x) - abs(y)) % 2 != 0) {         puts( -1 );         continue;       }       int lo = 1, hi = n;       vector<int> aux(256);       while (lo <= hi) {         int mi = (lo + hi) >> 1;         bool ok = false;         for (int i = 1; i + mi - 1 <= n; ++i) {           for (auto& e : DIRS) aux[e] = get(e, 1, i - 1) + get(e, i + mi, n);           int xx = aux[ R ] - aux[ L ];           int yy = aux[ U ] - aux[ D ];           int rem = mi - (abs(x - xx) + abs(y - yy));           if (rem >= 0) ok = true;         }         if (ok)           hi = mi - 1;         else           lo = mi + 1;       }       printf( %d n , lo);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200010], b[200010];   bool used[200010];   int main() {     int n, k = 0, i, x;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     for (i = 1; i <= n; i++) scanf( %d , &b[i]);     int ans = 1 - accumulate(b + 1, b + n + 1, 0) % 2;     for (i = 1; i <= n; i++)       if (!used[i]) {         used[i] = true;         x = a[i];         while (!used[x]) {           used[x] = true;           x = a[x];         }         k++;       }     if (k > 1) ans += k;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m, i, cnt = 0, sum = 0, ans = 0, x, j;     string s;     vector<string> v;     cin >> n >> m;     int a[m];     for (i = 0; i < n; i++) {       cin >> s;       v.push_back(s);     }     for (i = 0; i < m; i++) cin >> a[i];     for (i = 0; i < m; i++) {       int arr[5] = {0};       for (j = 0; j < n; j++) {         x = v[j][i] -  A ;         arr[x]++;       }       sort(arr, arr + 5);       ans += arr[4] * a[i];     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   void chekarre(int* arr, int n) {     cout <<  [ ;     for (int i = 0; i < n; i++) cout << arr[i] <<    ;     cout <<  ]  << endl;   }   bool comp(int i, int j) { return (i > j); }   int a[2000007];   int main() {     int n;     long long sum = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     reverse(a, a + n);     while (n) {       for (int j = 0; j < n; j++) {         sum += a[j];       }       n = n / 4;     }     cout << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b;   int ans, nn;   char x[100010];   int change(int t) {     int rtn = 0, bit = 1;     while (t) {       rtn += (t % 10) * bit;       bit *= ans;       t /= 10;     }     return rtn;   }   int main() {     scanf( %d%d , &a, &b);     int aa = a, bb = b;     while (aa) {       ans = max(ans, aa % 10);       aa /= 10;     }     while (bb) {       ans = max(ans, bb % 10);       bb /= 10;     }     ans += 1;     a = change(a);     b = change(b);     a += b;     while (a) {       x[nn++] = a % ans +  0 ;       a /= ans;     }     printf( %d , nn);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long OO = 1e8;   const double EPS = (1e-7);   int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }   const int N = 100;   bool adjMatrixBool[N][N];   int adjMatrix[N][N];   vector<int> adjMatrixAll[N][N];   map<pair<int, int>, int> adjMatrixMap;   vector<vector<int> > adjList1;   vector<vector<pair<int, int> > > adjList2;   vector<bool> visited;   bool valid(int i, int j) { return 1; }   char arr[303][303], x, b;   bool prime(long long n) {     long long i;     for (i = 2; i <= sqrt(n); i++) {       if (n % i == 0) return false;     }     return true;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }   int main() {     ios_base::sync_with_stdio(false);     long long n;     cin >> n;     string s;     cin >> s;     long long arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     long long ans = INT_MAX;     for (int i = 0; i < n - 1; i++) {       if (s[i] ==  R  and s[i + 1] ==  L )         ans = min(ans, abs((arr[i + 1] - arr[i]) / 2));     }     if (ans == INT_MAX)       cout <<  -1  << endl;     else       cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const long long INF = 1000 * 1000 * 1000 + 7;   const long long LINF = INF * (long long)INF;   const int MOD = 1000 * 1000 * 1000 + 7;   const int MAX = 10000 + 47;   long long F[MAX];   vector<int> R;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int s, k;     cin >> s >> k;     int p = 0;     F[0] = 1;     for (int i = (1); i < (MAX); i++) {       for (int j = (max(0, i - k)); j < (i); j++) {         F[i] += F[j];       }       if (F[i] > s) {         p = i;         break;       }     }     for (int i = (p)-1; i >= (0); i--) {       if (s >= F[i]) {         s -= F[i];         R.push_back(F[i]);       }     }     if ((int)R.size() == 1) R.push_back(0);     cout << (int)R.size() << endl;     for (int i = (0); i < ((int)R.size()); i++) {       cout << R[i] <<    ;     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const double PI = acos(-1.0);   const long long int mod = 1e9 + 7;   const int MAXN = 1e6 + 5;   void cp() {     long long int n, i, k;     cin >> n >> k;     long long int a[n], sum = 0;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (i = 1; i < n; i++) {       if (a[i] < k) {         sum += (k - a[i]) / a[0];       }     }     cout << sum;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     t = 1;     cin >> t;     while (t--) {       cp();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const long long INFL = LLONG_MAX;   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   int main() {     ios_base::sync_with_stdio(0);     cout.precision(15);     cout << fixed;     cout.tie(0);     cin.tie(0);     int n, k;     cin >> n >> k;     int p = 2 * n + 1;     int a[p + 2];     for (int i = 1; i <= p; i++) cin >> a[i];     a[0] = -INF;     a[p + 1] = +INF;     for (int i = 1; i <= p && k > 0; i++)       if (i % 2 == 0 && a[i] > a[i - 1] && a[i] > a[i + 1] &&           a[i] - a[i - 1] > 1 && a[i] - a[i + 1] > 1) {         k--;         a[i]--;       }     for (int i = 1; i <= p; i++) cout << a[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[300005];   long long c[300005] = {0};   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     int i;     for (i = 1; i <= n - 1; i++) cin >> a[i];     long long m = 0;     long long r = 0;     for (i = 1; i <= n - 1; i++) {       r += a[i];       m = min(m, r);     }     m = abs(m) + 1;     long long y = m;     if (m > n || c[m]) {       cout << -1;       return 0;     }     c[m] = 1;     for (i = 1; i <= n - 1; i++) {       m += a[i];       if (m > n || c[m]) {         cout << -1;         return 0;       }       c[m] = 1;     }     cout << y <<    ;     for (i = 1; i <= n - 1; i++) {       y += a[i];       cout << y <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int myrand(int m) { return (rand() * (1 << 15) + rand()) % m; }   int main() {     srand(time(0));     int n, ind, x;     cin >> n >> ind >> x;     --ind;     vector<int> left(n);     vector<bool> vis(n, 0);     for (int i = 0; i < n; ++i) left[i] = i;     vis[ind] = 1;     random_shuffle(left.begin(), left.end());     int best = -1;     for (int i = 0; i < min(1000, n); ++i) {       int v, ii = left[i];       cout <<  ?   << ii + 1 <<   n ;       fflush(stdout);       cin >> v >> ii;       if (v < x && v > best) {         ind = left[i];         best = max(best, v);       }     }     if (ind < 0) {       cout <<  ! -1 n ;       fflush(stdout);       return 0;     }     int itt, LIM = 999;     for (itt = 0; itt < LIM; ++itt) {       cout <<  ?   << ind + 1 <<   n ;       fflush(stdout);       int v;       cin >> v >> ind;       --ind;       if (v >= x) {         best = v;         break;       }       if (ind < 0) break;     }     if (itt == LIM) {       int j = 1;       while (1) {         j = j + 1;       }     }     cout <<  !   << ((best < x) ? -1 : best) <<   n ;     fflush(stdout);   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long n;     cin >> n;     map<string, long long> count;     for (long long i = 0; i < n; i++) {       string s;       cin >> s;       count[s]++;     }     string s1 =  a , s2 =  a ;     for (map<string, long long>::iterator it = count.begin(); it != count.end();          ++it) {       if (s1 ==  a )         s1 = (*it).first;       else         s2 = (*it).first;     }     if (count[s1] > count[s2])       cout << s1 <<   n ;     else       cout << s2 <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int to(int x, int y) { return (x * 60) + (y); }   long long int gcd(long long int a, long long int b) {     return b == 0 ? a : gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int arr[n + 1];     for (int i = 1; i <= n; i++) cin >> arr[i];     int mx = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (arr[i] == i and mx <= i) {         ans++;         continue;       }       if (arr[i] == i) {         continue;       }       mx = max(arr[i], mx);     }     cout << ans;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   int N;   char s[507][507];   int rX[] = {-1, 0, 1, 0};   int rY[] = {0, -1, 0, 1};   int spoj[507][507];   int rozm[1000007];   int akt[1000007];   int terRozm;   int wyn;   void dfs(int y, int x, int num) {     spoj[y][x] = num;     terRozm++;     for (int i = 0; i < 4; i++) {       int nx = x + rX[i], ny = y + rY[i];       if (nx >= 1 && nx <= N && ny >= 1 && ny <= N) {         if (s[ny][nx] ==  .  && spoj[ny][nx] == 0) dfs(ny, nx, num);       }     }   }   void dolicz(int y1, int x1, int y2, int x2, bool kk = false) {     for (int y = y1; y <= y2; y++) {       if (y < 1 || y > N) continue;       for (int x = x1; x <= x2; x++) {         if (x < 1 || x > N) continue;         if (s[y][x] ==  . ) {           int sp = spoj[y][x];           akt[sp]++;           if (akt[sp] == 1) wyn += rozm[sp];         } else if (kk) {           wyn++;         }       }     }   }   void odejmij(int y1, int x1, int y2, int x2, bool kk = false) {     for (int y = y1; y <= y2; y++) {       if (y < 1 || y > N) continue;       for (int x = x1; x <= x2; x++) {         if (x < 1 || x > N) continue;         if (s[y][x] ==  . ) {           int sp = spoj[y][x];           akt[sp]--;           if (akt[sp] == 0) wyn -= rozm[sp];         } else if (kk) {           wyn--;         }       }     }   }   int main() {     int K;     scanf( %d%d , &N, &K);     for (int i = 1; i <= N; i++) scanf( %s , s[i] + 1);     int ileSpoj = 0;     for (int i = 1; i <= N; i++)       for (int w = 1; w <= N; w++)         if (s[i][w] ==  .  && spoj[i][w] == 0) {           terRozm = 0;           dfs(i, w, ++ileSpoj);           rozm[ileSpoj] = terRozm;         }     int maxi = 0;     for (int i = 1; i <= N - K + 1; i++) {       dolicz(i, 1, i + K - 1, K, true);       dolicz(i - 1, 1, i - 1, K);       dolicz(i + K, 1, i + K, K);       dolicz(i, 0, i + K - 1, 0);       dolicz(i, K + 1, i + K - 1, K + 1);       maxi = std::max(wyn, maxi);       for (int x = 2; x <= N + 2; x++) {         odejmij(i, x - 2, i + K - 1, x - 2);         odejmij(i, x - 1, i + K - 1, x - 1, true);         dolicz(i, x - 1, i + K - 1, x - 1);         odejmij(i, x + K - 1, i + K - 1, x + K - 1);         dolicz(i, x + K - 1, i + K - 1, x + K - 1, true);         dolicz(i, x + K, i + K - 1, x + K);         odejmij(i - 1, x - 1, i - 1, x - 1);         dolicz(i - 1, x + K - 1, i - 1, x + K - 1);         odejmij(i + K, x - 1, i + K, x - 1);         dolicz(i + K, x + K - 1, i + K, x + K - 1);         maxi = std::max(wyn, maxi);       }     }     printf( %d , maxi);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, c, a[200001], tin[200001], tout[200001], sz[200001], seg[524288];   string s;   bool lazy[524288];   vector<int> G[200001];   void dfs(int i) {     tin[i] = ++c;     ++sz[i];     for (int j : G[i]) {       dfs(j);       sz[i] += sz[j];     }     tout[i] = c;   }   void upd(int l, int r, int s = 1, int e = n, int i = 1) {     if (lazy[i]) {       seg[i] = (e - s + 1) - seg[i];       if (s != e) {         lazy[2 * i] ^= 1;         lazy[2 * i + 1] ^= 1;       }       lazy[i] = 0;     }     if (s > r || e < l) return;     if (s >= l && e <= r) {       seg[i] = (e - s + 1) - seg[i];       if (s != e) {         lazy[2 * i] ^= 1;         lazy[2 * i + 1] ^= 1;       }       return;     }     int mid = (s + e) / 2;     upd(l, r, s, mid, 2 * i);     upd(l, r, mid + 1, e, 2 * i + 1);     seg[i] = seg[2 * i] + seg[2 * i + 1];   }   int que(int l, int r, int s = 1, int e = n, int i = 1) {     if (lazy[i]) {       seg[i] = (e - s + 1) - seg[i];       if (s != e) {         lazy[2 * i] ^= 1;         lazy[2 * i + 1] ^= 1;       }       lazy[i] = 0;     }     if (s > r || e < l) return 0;     if (s >= l && e <= r) return seg[i];     int mid = (s + e) / 2;     return que(l, r, s, mid, 2 * i) + que(l, r, mid + 1, e, 2 * i + 1);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 2; i <= n; ++i) {       cin >> x;       G[x].push_back(i);     }     for (int i = 1; i <= n; ++i) cin >> a[i];     dfs(1);     for (int i = 1; i <= n; ++i)       if (a[i]) upd(tin[i], tin[i]);     cin >> m;     while (m--) {       cin >> s >> x;       if (s ==  pow )         upd(tin[x], tout[x]);       else         cout << que(tin[x], tout[x]) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e8 + 1;   vector<vector<int> > adj;   vector<bool> visited;   int contadorNodos = 0;   void DFS(int nodo) {     contadorNodos++;     for (auto it : adj[nodo]) {       DFS(it);     }   }   void letras(string &s) {     for (int i = 0; i < s.size(); i++) {       s[i] = tolower(s[i]);     }   }   int main() {     int n;     cin >> n;     map<string, int> m;     adj.resize(205);     visited.resize(205, false);     int j = 1;     for (int i = 0; i < n; i++) {       string a, b, c;       cin >> a >> b >> c;       letras(a);       letras(c);       if (m[a] == 0) {         m[a] = j;         j++;       }       if (m[c] == 0) {         m[c] = j;         j++;       }       adj[m[a]].push_back(m[c]);     }     int res = 0;     for (int i = 1; i < j; i++) {       contadorNodos = 0;       DFS(i);       res = max(res, contadorNodos);     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   const bool online_judge = true;   const long long inf = 1LL << 60;   long long toInt(string s) {     long long res;     stringstream ss;     ss << s;     ss >> res;     return res;   }   string toString(long long n) {     stringstream ss;     ss << n;     return ss.str();   }   long long w, h;   vector<string> f;   typedef struct {     long long x;     long long y;     long long dp;     long long cp;   } state;   state memo[55][55][5][5];   long long dx[4] = {0, 1, 0, -1};   long long dy[4] = {-1, 0, 1, 0};   long long ex[4] = {-1, 0, 1, 0};   long long ey[4] = {0, -1, 0, 1};   state solve(long long y, long long x, long long dp, long long cp) {     if (memo[y][x][dp][cp].x != -1) return memo[y][x][dp][cp];     state ans;     long long nx = x + dx[dp];     long long ny = y + dy[dp];     long long mx = x + ex[(dp + (cp ? 2 : 0)) % 4];     long long my = y + ey[(dp + (cp ? 2 : 0)) % 4];     if (f[ny][nx] == f[y][x]) {       ans = solve(ny, nx, dp, cp);     } else if (f[my][mx] == f[y][x]) {       ans = solve(my, mx, dp, cp);     } else {       char c = f[ny][nx];       if (c ==  0 ) {         if (cp == 0) {           ans.y = y;           ans.x = x;           ans.dp = dp;           ans.cp = 1;         } else {           ans.y = y;           ans.x = x;           ans.dp = (dp + 1) % 4;           ans.cp = 0;         }       } else {         ans.x = nx;         ans.y = ny;         ans.dp = dp;         ans.cp = cp;       }     }     memo[y][x][dp][cp] = ans;     return ans;   }   void run() {     long long m, n;     cin >> m >> n;     for (long long _ = 0; _ < m; _++) {       string tmp;       cin >> tmp;       f.push_back(tmp);     }     w = f[0].size();     h = m;     for (long long i = 0; i < m; i++) {       f[i] =  0  + f[i] +  0 ;     }     f.insert(f.begin(), string(w + 2,  0 ));     f.push_back(string(w + 2,  0 ));     for (long long i = 1; i <= h; i++) {       for (long long j = 1; j <= w; j++) {         for (long long dp = 0; dp <= 3; dp++) {           for (long long cp = 0; cp <= 1; cp++) {             memo[i][j][dp][cp].x = -1;           }         }       }     }     for (long long i = 1; i <= h; i++) {       for (long long j = 1; j <= w; j++) {         for (long long dp = 0; dp <= 3; dp++) {           for (long long cp = 0; cp <= 1; cp++) {             solve(i, j, dp, cp);           }         }       }     }     long long x = 1, y = 1, dp = 1, cp = 0;     while (n--) {       state next = memo[y][x][dp][cp];       x = next.x;       y = next.y;       dp = next.dp;       cp = next.cp;     }     cout << f[y][x] << endl;   }   int main(int argc, char *argv[]) {     run();     return 0;   }