Problem p01437 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  string s = "ESWN";
  int n, m;
  ll t;
  while (cin >> n >> m >> t, n + m + t) {
    vs in(n);
    rep(i, n) cin >> in[i];
    int x, y, now;
    rep(i, n) rep(j, m) rep(k, 4) if (in[i][j] == s[k]) x = i, y = j, now = k;
    ll memo[101][101][4] = {0};
    bool h = false;
    while (t--) {
      if (!h && memo[x][y][now]) {
        h = true;
        t %= memo[x][y][now] - t;
      }
      memo[x][y][now] = t;
      int nx = x + dx[now];
      int ny = y + dy[now];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m || in[nx][ny] == '#')
        (now += 1) %= 4, t++;
      else
        x = nx, y = ny;
    }
    cout << x + 1 << " " << y + 1 << " " << s[now] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  string s = "ESWN";
  int n, m, t;
  while (cin >> n >> m >> t, n + m + t) {
    vs in(n);
    rep(i, n) cin >> in[i];
    int x, y, now;
    rep(i, n) rep(j, m) rep(k, 4) if (in[i][j] == s[k]) x = i, y = j, now = k;
    int memo[101][101][4] = {0};
    int T = t;
    while (t--) {
      if (memo[x][y][now]) {
        t %= memo[x][y][now] - t + 1;
      }
      memo[x][y][now] = t;
      int nx = x + dx[now];
      int ny = y + dy[now];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m || in[nx][ny] == '#')
        (now += 1) %= 4, t++;
      else
        x = nx, y = ny;
    }
    cout << x + 1 << " " << y + 1 << " " << s[now] << endl;
  }
}