Language: C++

#include<bits/stdc++.h>
using namespace std;
#define MAX 55
#define INF 1000000
#define inf 10000000
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};

int H,W;
char a[MAX][MAX];
string s;
int p,ans,len,C;
int py,px,ty,tx;
int dir,flg;

char vd[4][MAX][MAX][1005];
char ny[4][MAX][MAX][1005];
char nx[4][MAX][MAX][1005];
char ndir[4][MAX][MAX][1005];

int mem[1005];
int search(int x){
  if(mem[x])return mem[x];
  
  char L=s[x],R;
  if(L=='{')R='}';
  if(L=='[')R=']';
  int cnt=1;
  for(int i=x+1;i<len;i++){
    if(s[i]==L)cnt++;
    if(s[i]==R)cnt--;
    if(cnt==0)return mem[x]=i+1;
  }
}

void func(char ch){
  ans++;
  if(ch=='<')dir=(dir+3)%4;
  else if(ch=='>')dir=(dir+1)%4;
  else if(ch=='^'||ch=='v'){
    int k=-1;
    if(ch=='^')k=1;
    int ny=py+dy[dir]*k;
    int nx=px+dx[dir]*k;
    if(a[ny][nx]!='#')py=ny,px=nx;
    if(py==ty&&px==tx&&flg==0){
      cout<<ans<<endl;
      flg=1;
    }
  }
}

bool check(){
  char ch=s[p++];
  if(ch=='~')return !check();
  if(ch=='T')return true;
  if(ch=='N')return (dir==0);
  if(ch=='E')return (dir==1);
  if(ch=='S')return (dir==2);
  if(ch=='W')return (dir==3);
  int ny=py+dy[dir];
  int nx=px+dx[dir];
  return (a[ny][nx]=='#');
}

void solve();

void calc(){
  C++;
  if(flg || ans>=INF || C>=inf)return;
  char ch=s[p++];
  if(ch=='['){
    int np=search(p-1);
    int& dir2=ndir[dir][py][px][p-1];
    int& py2=ny[dir][py][px][p-1];
    int& px2=nx[dir][py][px][p-1];
    if(vd[dir][py][px][p-1]){
      dir=dir2;py2=py;px2=px;
    }else{
      vd[dir][py][px][p-1]=true;
      if(check())solve();
      if(flg || ans>=INF || C>=inf)return;
      dir2=dir;py2=py;px2=px;
    }
    p=np;
  }else if(ch=='{'){
    int b=p,np=search(p-1);
    int& dir2=ndir[dir][py][px][p-1];
    int& py2=ny[dir][py][px][p-1];
    int& px2=nx[dir][py][px][p-1];
    if(vd[dir][py][px][p-1]){
      dir=dir2;py2=py;px2=px;
    }else{
      while(check()){
        solve();
        if(flg || ans>=INF || C>=inf)return;
        p=b;
      }
      dir2=dir;py2=py;px2=px;
    }
    p=np;
  }else func(ch);
}

void solve(){
  C++;
  while(p<len){
    if(s[p]=='}')break;
    if(s[p]==']')break;
    calc();
    if(flg || ans>=INF || C>=inf)return;
  }
}

int main(){
  cin>>H>>W;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>a[i][j];
      if(a[i][j]=='s'){
        a[i][j]='.';
        py=i,px=j;
      }else if(a[i][j]=='g'){
        a[i][j]='.';
        ty=i,tx=j;
      }
    }
  }
  cin>>s;
  len=s.size();
  p=ans=dir=flg=C=0;
  solve();
  if(!flg)cout<<-1<<endl;
  return 0;
}

Compiler output:
a.cc: In function 'void calc()':
a.cc:74:36: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'char'
   74 |     int& dir2=ndir[dir][py][px][p-1];
      |               ~~~~~~~~~~~~~~~~~~~~~^
a.cc:75:33: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'char'
   75 |     int& py2=ny[dir][py][px][p-1];
      |              ~~~~~~~~~~~~~~~~~~~^
a.cc:76:33: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'char'
   76 |     int& px2=nx[dir][py][px][p-1];
      |              ~~~~~~~~~~~~~~~~~~~^
a.cc:88:36: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'char'
   88 |     int& dir2=ndir[dir][py][px][p-1];
      |               ~~~~~~~~~~~~~~~~~~~~~^
a.cc:89:33: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'char'
   89 |     int& py2=ny[dir][py][px][p-1];
      |              ~~~~~~~~~~~~~~~~~~~^
a.cc:90:33: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'char'
   90 |     int& px2=nx[dir][py][px][p-1];
      |              ~~~~~~~~~~~~~~~~~~~^
a.cc: In function 'int search(int)':
a.cc:34:1: warning: control reaches end of non-void function [-Wreturn-type]
   34 | }
      | ^