#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = (1 << 30 ) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] ={1,0,-1,0,1,-1,1,-1};
ll dy[8] ={0,1,0,-1,1,-1,-1,1};
 
template<class T> 
inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> 
inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


void solve(){
    ll n;
    ll x[200010],y[200010];
    char u[200010];
    set<ll> v[200010],w[200010];
    ll ans=LLINF;
    cin>>n;
    rep(i,n){
        cin>>x[i]>>y[i]>>u[i];
    }
    rep(i,n){
        if(u[i]=='D') v[x[i]].insert(y[i]);
        else if(u[i]=='L') w[y[i]].insert(x[i]);
    }
    rep(i,n){
        if(u[i]=='U'){
            if(v[x[i]].lower_bound(y[i])==v[x[i]].end()) continue;
            ll z=*v[x[i]].lower_bound(y[i]);
            chmin(ans,(z-y[i])*5);
        }
        else if(u[i]=='R'){
            if(w[y[i]].lower_bound(x[i])==w[y[i]].end()) continue;
            ll z=*w[y[i]].lower_bound(x[i]);
            chmin(ans,(z-x[i])*5);
        }
    }

    set<pll> a[400010],b[400010];
    set<pll> c[400010],d[400010];
    rep(i,n){
        ll X=x[i]+y[i],Y=x[i]-y[i]+200000;
        if(u[i]=='U'){
            a[X].insert({x[i],y[i]});
            
        }
        else if(u[i]=='D'){
            c[Y].insert({x[i],y[i]});
            
        }
        else if(u[i]=='L'){
            b[X].insert({x[i],y[i]});
            d[Y].insert({x[i],y[i]});

        }
    }
    rep(i,n){
        ll X=x[i]+y[i],Y=x[i]-y[i]+200000;
        if(u[i]=='R'){
            if(a[X].lower_bound({x[i],y[i]})==a[X].end()){}
            else {
                pll z=*a[X].lower_bound({x[i],y[i]});
                chmin(ans,(y[i]-z.sc+z.fi-x[i])*5);
            }

            if(c[Y].lower_bound({x[i],y[i]})==c[Y].end()){}
            else {
                pll z=*c[Y].lower_bound({x[i],y[i]});
                chmin(ans,(z.sc-y[i]+z.fi-x[i])*5);
            }
        }
        else if(u[i]=='U'){
            if(d[Y].lower_bound({x[i],y[i]})==d[Y].end()){}
            else{
                pll z=*d[Y].lower_bound({x[i],y[i]});
                chmin(ans,(z.fi-x[i]+z.sc-y[i])*5);
            }
        }
        else if(u[i]=='D'){
            if(b[X].lower_bound({x[i],y[i]})==b[X].end()){}
            else {
                pll z=*b[X].lower_bound({x[i],y[i]});
                chmin(ans,(z.fi-x[i]+y[i]-z.sc)*5);
            }
        }

    }

   

    if(ans==LLINF) cout<<"SAFE"<<endl;
    else cout<<ans<<endl;





}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    
     

    solve(); 


    return 0;
}

// This code wrote by chtholly_micromaker(MicroMaker)
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define reg register
using namespace std;
const int MaxN=2050;
const int dir[4][2]={1,0,0,1,-1,0,0,-1};
template <class t> inline void read(t &s)
{
	s=0;
	reg int f=1;
	reg char c=getchar();
	while(!isdigit(c))
	{
		if(c=='-')
			f=-1;
		c=getchar();
	}
	while(isdigit(c))
		s=(s<<3)+(s<<1)+(c^48),c=getchar();
	s*=f;
	return;
}
char G[MaxN][MaxN];
int f[MaxN][MaxN],g[MaxN][MaxN],h[MaxN][MaxN];
int n,m;
signed main(void)
{
	// freopen("big.in","r",stdin);
	int q;
	cin>>n>>m>>q;
	for(int i=1;i<=n;++i)
		scanf("%s",G[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+(G[i][j]=='1'?1:0);
			g[i][j]=g[i-1][j]+g[i][j-1]-g[i-1][j-1];
			h[i][j]=h[i-1][j]+h[i][j-1]-h[i-1][j-1];
			if(G[i][j]=='1')
			{
				if(G[i-1][j]=='1')
					++g[i][j];
				if(G[i][j-1]=='1')
					++h[i][j];
			}
		}
	reg int U,V,X,Y;
	for(int qq=0;qq<q;++qq)
	{
		read(U);read(V);read(X);read(Y);
		reg int ans=f[X][Y]-f[U-1][Y]-f[X][V-1]+f[U-1][V-1];
		// printf("t1 %d\n",f[X][Y]-f[U-1][Y]-f[X][V-1]+f[U-1][V-1]);
		ans-=g[X][Y]-g[U][Y]-g[X][V-1]+g[U][V-1];
		// printf("t2 %d\n",g[X][Y]-g[U][Y]-g[X][V-1]+g[U][V-1]);
		ans-=h[X][Y]-h[U-1][Y]-h[X][V]+h[U-1][V];
		// printf("t3 %d\n",h[X][Y]-h[U-1][Y]-h[X][V]+h[U-1][V]);
		printf("%d\n",ans);
	}
	return 0;
}

