OIG IV - waz

// https://szkopul.edu.pl/problemset/problem/emwyiI5xOK9Cg3RGeF4fkJUA/site/?key=statement
// OIG IV (1 etap)

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits.h>
#include <vector>

constexpr int sizik = 1024;

struct Point {
    int time = INT16_MIN;
    // int time = -1;
};

struct Coord {
    int x, y, k;
    Coord(int x, int y, char k) {
        this->x = x;
        this->y = y;
        this->k = k;
    }
};

Point map[sizik][sizik];
bool jedzenie[sizik][sizik];

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int n, m, r;
    std::cin >> n >> m >> r;

    char initial_k;
    std::cin >> initial_k;

    Coord curr_pos(1, 1, 'N');
    int curr_time = 0, snake_length = 1;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char a;
            std::cin >> a;

            if (a == '.') {
                continue;
            } else if (a == 'J') {
                jedzenie[i][j] = true;
            } else if (a == 'W') {
                curr_pos = {j, i, initial_k};
                map[i][j].time = 0;
            }
        }
    }

    for (int i = 1; i <= r; i++) {
        char a;
        std::cin >> a;

        if (a == 'P') {
            if (curr_pos.k == 'N') {
                curr_pos.k = 'E';
            } else if (curr_pos.k == 'E') {
                curr_pos.k = 'S';
            } else if (curr_pos.k == 'S') {
                curr_pos.k = 'W';
            } else if (curr_pos.k == 'W') {
                curr_pos.k = 'N';
            }
        } else if (a == 'L') {
            if (curr_pos.k == 'N') {
                curr_pos.k = 'W';
            } else if (curr_pos.k == 'W') {
                curr_pos.k = 'S';
            } else if (curr_pos.k == 'S') {
                curr_pos.k = 'E';
            } else if (curr_pos.k == 'E') {
                curr_pos.k = 'N';
            }
        }

        curr_time++;

        if (curr_pos.k == 'N') {
            curr_pos.y--;
        } else if (curr_pos.k == 'S') {
            curr_pos.y++;
        } else if (curr_pos.k == 'W') {
            curr_pos.x--;
        } else if (curr_pos.k == 'E') {
            curr_pos.x++;
        }

        if (curr_pos.x < 1) {
            std::cout << curr_time << '\n';
            return 0;
        }

        if (curr_pos.y < 1) {
            std::cout << curr_time << '\n';
            return 0;
        }

        if (curr_pos.x > m) {
            std::cout << curr_time << '\n';
            return 0;
        }

        if (curr_pos.y > n) {
            std::cout << curr_time << '\n';
            return 0;
        }

        if (curr_time - map[curr_pos.y][curr_pos.x].time <= snake_length) {
            std::cout << curr_time << '\n';
            return 0;
        }

        map[curr_pos.y][curr_pos.x].time = curr_time;

        if (jedzenie[curr_pos.y][curr_pos.x]) {
            jedzenie[curr_pos.y][curr_pos.x] = false;
            snake_length++;
        }
    }

    std::cout << "OK\n";

    return 0;
}