#ifdef NACHIA #define _GLIBCXX_DEBUG #else // disable assert #define NDEBUG #endif #include #include #include #include using namespace std; using ll = long long; const ll INF = 1ll << 60; #define REP(i,n) for(ll i=0; i using V = vector; template void chmax(A& l, const B& r){ if(l < r) l = r; } template void chmin(A& l, const B& r){ if(r < l) l = r; } struct Ans { pair S = {0,0}, T = {0,0}; string command; bool ok = 0; }; ll dx[4] = {0,1,0,-1}; ll dy[4] = {-1,0,1,0}; Ans solve(ll H, ll W, V A){ ll yp = -1, xp = -1, d = 0; Ans ans; for(ll x=W-1; x>=0; x--) REP(y,H) if(A[y][x] == '.' && y > 0 && A[y-1][x] == '.'){ yp = y; xp = x; } if(yp < 0) return ans; auto onBoard = [&](ll y, ll x){ return 0 <= y && y < H && 0 <= x && x < W; }; auto test = [&](ll y, ll x, ll d){ y += dy[d]; x += dx[d]; if(!onBoard(y,x)) return '#'; return A[y][x]; }; ans.S = {yp, xp}; // cout<< "solve" << endl; // for(auto& a : A) cout << a << endl; while(1){ // cout << "y = " << yp << " , x = " << xp << " , d = " << d << endl; A[yp][xp] = '#'; if(test(yp, xp, d) == '.'){ ans.command.push_back('F'); } else if(test(yp, xp, (d+1)%4) == '.'){ ans.command.push_back('R'); d = (d+1)%4; } else{ break; } yp += dy[d]; xp += dx[d]; } REP(y,H) REP(x,W) if(A[y][x] != '#') return ans; // cout << "OK" << endl; ans.ok = 1; ans.T = {yp, xp}; return ans; } void testcase(){ ll H, W; cin >> H >> W; V A(H); REP(y,H) cin >> A[y]; bool done = 0; ll rev = 0; ll cy = 0; Ans ans; { ll cnt = 0; REP(y,H) REP(x,W) if(A[y][x] == '.') cnt++; if(cnt == 1){ REP(y,H) REP(x,W) if(A[y][x] == '.'){ cout << (y+1) << " " << (x+1) << " " << "U" << "\n"; cout << 0 << "\n"; cout << "\n"; return; } } } REP(sw,2){ REP(tt,4){ if(!done){ auto ansbuf = solve(H, W, A); if(ansbuf.ok){ done = 1; ans = ansbuf; cy = tt; rev = sw; } } V buf(W, string(H, '.')); REP(y,H) REP(x,W) buf[W-1-x][y] = A[y][x]; ans.S = {W-1-ans.S.second, ans.S.first}; ans.T = {W-1-ans.T.second, ans.T.first}; swap(H, W); swap(A, buf); } reverse(A.begin(), A.end()); } if(!done){ cout << "-1\n"; return; } if(rev){ reverse(ans.command.begin(), ans.command.end()); ans.command = "F" + ans.command.substr(0, ans.command.size() - 1); ans.S.first = H - 1 - ans.S.first; ans.T.first = H - 1 - ans.T.first; swap(ans.S, ans.T); if(cy % 2 == 1) cy = (cy + 2) % 4; } cout << (ans.S.first + 1) << " " << (ans.S.second + 1) << " " << (char)("URDL"[cy]) << "\n"; cout << ans.command.size() << "\n"; cout << ans.command << "\n"; } int main(){ cin.tie(0)->sync_with_stdio(0); testcase(); return 0; }