#include<iostream>
using namespace std;
int main(){
    int w=1,h=1,i,j,k,dp[111][111][2],p,flag[111];
    char s[111][111];
    while(1){
        cin>>w>>h;
if(w==0)
return 0;
    for(i=0;i<111;i++){
        for(j=0;j<111;j++)
        s[i][j]='X';
    }
    for(i=10;i-10<h;i++){
        for(j=10;j-10<w;j++){
            dp[i][j][0]=100000000;
            dp[i][j][1]=100000000;
            cin>>s[i][j];
            if(s[i][j]=='S'){
                dp[i][j][0]=0;
                dp[i][j][1]=0;
                s[i][j]='0';
            }
        }
    }
    for(k=0;k<h*w/2;k++){
        for(i=10;i-10<h;i++){
            for(j=10;j-10<w;j++){
                if(s[i][j+1]!='X'){
                    p=s[i][j+1]-'0';
                    if(dp[i][j][0]+p<dp[i][j+1][1])
                    dp[i][j+1][1]=dp[i][j][0]+p;
                }
                if(s[i][j+2]!='X'){
                    p=s[i][j+2]-'0';
                    if(dp[i][j][0]+p<dp[i][j+2][1])
                    dp[i][j+2][1]=dp[i][j][0]+p;
                }
                if(s[i][j+3]!='X'){
                    p=s[i][j+3]-'0';
                    if(dp[i][j][0]+p<dp[i][j+3][1])
                    dp[i][j+3][1]=dp[i][j][0]+p;
                }
                if(s[i+1][j+1]!='X'){
                    p=s[i+1][j+1]-'0';
                    if(dp[i][j][0]+p<dp[i+1][j+1][1])
                    dp[i+1][j+1][1]=dp[i][j][0]+p;
                }
                if(s[i+1][j+2]!='X'){
                    p=s[i+1][j+2]-'0';
                    if(dp[i][j][0]+p<dp[i+1][j+2][1])
                    dp[i+1][j+2][1]=dp[i][j][0]+p;
                }
                if(s[i+2][j+1]!='X'){
                    p=s[i+2][j+1]-'0';
                    if(dp[i][j][0]+p<dp[i+2][j+1][1])
                    dp[i+2][j+1][1]=dp[i][j][0]+p;
                }
                if(s[i-1][j+1]!='X'){
                    p=s[i-1][j+1]-'0';
                    if(dp[i][j][0]+p<dp[i-1][j+1][1])
                    dp[i-1][j+1][1]=dp[i][j][0]+p;
                }
                if(s[i-1][j+2]!='X'){
                    p=s[i-1][j+2]-'0';
                    if(dp[i][j][0]+p<dp[i-1][j+2][1])
                    dp[i-1][j+2][1]=dp[i][j][0]+p;
                }
                if(s[i-2][j+1]!='X'){
                    p=s[i-2][j+1]-'0';
                    if(dp[i][j][0]+p<dp[i-2][j+1][1])
                    dp[i-2][j+1][1]=dp[i][j][0]+p;
                }
                if(s[i][j-1]!='X'){
                    p=s[i][j-1]-'0';
                    if(dp[i][j][1]+p<dp[i][j-1][0])
                    dp[i][j-1][0]=dp[i][j][1]+p;
                }
                if(s[i][j-2]!='X'){
                    p=s[i][j-2]-'0';
                    if(dp[i][j][1]+p<dp[i][j-2][0])
                    dp[i][j-2][0]=dp[i][j][1]+p;
                }
                if(s[i][j-3]!='X'){
                    p=s[i][j-3]-'0';
                    if(dp[i][j][1]+p<dp[i][j-3][0])
                    dp[i][j-3][0]=dp[i][j][1]+p;
                }
                if(s[i+1][j-1]!='X'){
                    p=s[i+1][j-1]-'0';
                    if(dp[i][j][1]+p<dp[i+1][j-1][0])
                    dp[i+1][j-1][0]=dp[i][j][1]+p;
                }
                if(s[i+1][j-2]!='X'){
                    p=s[i+1][j-2]-'0';
                    if(dp[i][j][1]+p<dp[i+1][j-2][0])
                    dp[i+1][j-2][0]=dp[i][j][1]+p;
                }
                if(s[i+2][j-1]!='X'){
                    p=s[i+2][j-1]-'0';
                    if(dp[i][j][1]+p<dp[i+2][j-1][0])
                    dp[i+2][j-1][0]=dp[i][j][1]+p;
                }
                if(s[i-1][j-1]!='X'){
                    p=s[i-1][j-1]-'0';
                    if(dp[i][j][1]+p<dp[i-1][j-1][0])
                    dp[i-1][j-1][0]=dp[i][j][1]+p;
                }
                if(s[i-1][j-2]!='X'){
                    p=s[i-1][j-2]-'0';
                    if(dp[i][j][1]+p<dp[i-1][j-2][0])
                    dp[i-1][j-2][0]=dp[i][j][1]+p;
                }
                if(s[i-2][j-1]!='X'){
                    p=s[i-2][j-1]-'0';
                    if(dp[i][j][1]+p<dp[i-2][j-1][0])
                    dp[i-2][j-1][0]=dp[i][j][1]+p;
                }
                
            }
        }
    }
    int ans=100000000;
    for(j=10;j-10<w;j++){
        if(s[10][j]=='T'){
            if(dp[10][j][0]<ans)
                ans=dp[10][j][0];
            if(dp[10][j][1]<ans)
                ans=dp[10][j][1];
        }
    }
    if(ans==100000000)
    cout<<"-1"<<endl;
    else
    cout<<ans-36<<endl;
    }
}


Measured memory_bytes: 3120