1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
using namespace std;

int mapa[2000][2000];
int stoki[2000][2000];

int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m,k,a,b,ile=0;
long long najm=0;
char znak;
cin>>n>>m>>k;
for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        cin>>znak;
        if(znak=='.'){
            if(j+1<m and (mapa[i][j+1]==0 or mapa[i][j]+1<mapa[i][j+1]) and (mapa[i][j]>0 or (i==0 and j==0))){
                mapa[i][j+1]=mapa[i][j]+1;
                stoki[i][j+1]=stoki[i][j]+1;
            }
            if(i+1<n and (mapa[i+1][j]==0 or mapa[i][j]+1<mapa[i+1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
                mapa[i+1][j]=mapa[i][j]+1;
                stoki[i+1][j]=stoki[i][j]+1;
            }
            if(j-1>=0 and (mapa[i][j-1]==0 or mapa[i][j]+1<mapa[i][j-1]) and (mapa[i][j]>0 or (i==0 and j==0))){
                mapa[i][j-1]=mapa[i][j]+1;
                stoki[i][j-1]=stoki[i][j];
            }
            if(i-1>=0 and (mapa[i-1][j]==0 or mapa[i][j]+1<mapa[i-1][j])  and (mapa[i][j]>0 or (i==0 and j==0))){
                mapa[i-1][j]=mapa[i][j]+1;
                stoki[i-1][j]=stoki[i][j];
            }
        }else{
            mapa[i][j]=-1;
        }
    }
    for(int j=m-1;j>=0;j--){
        if(j+1<m and (mapa[i][j+1]==0 or mapa[i][j]+1<mapa[i][j+1]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i][j+1]=mapa[i][j]+1;
            stoki[i][j+1]=stoki[i][j]+1;
        }
        if(i+1<n and (mapa[i+1][j]==0 or mapa[i][j]+1<mapa[i+1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i+1][j]=mapa[i][j]+1;
            stoki[i+1][j]=stoki[i][j]+1;
        }
        if(j-1>=0 and (mapa[i][j-1]==0 or mapa[i][j]+1<mapa[i][j-1]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i][j-1]=mapa[i][j]+1;
            stoki[i][j-1]=stoki[i][j];
        }
        if(i-1>=0 and (mapa[i-1][j]==0 or mapa[i][j]+1<mapa[i-1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i-1][j]=mapa[i][j]+1;
            stoki[i-1][j]=stoki[i][j];
        }
    }
}
for(int j=0;j<m;j++){
    for(int i=n-1;i>=0;i--){
        if(j+1<m and (mapa[i][j+1]==0 or mapa[i][j]+1<mapa[i][j+1]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i][j+1]=mapa[i][j]+1;
            stoki[i][j+1]=stoki[i][j]+1;
        }
        if(i+1<n and (mapa[i+1][j]==0 or mapa[i][j]+1<mapa[i+1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i+1][j]=mapa[i][j]+1;
            stoki[i+1][j]=stoki[i][j]+1;
        }
        if(j-1>=0 and (mapa[i][j-1]==0 or mapa[i][j]+1<mapa[i][j-1]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i][j-1]=mapa[i][j]+1;
            stoki[i][j-1]=stoki[i][j];
        }
        if(i-1>=0 and (mapa[i-1][j]==0 or mapa[i][j]+1<mapa[i-1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i-1][j]=mapa[i][j]+1;
            stoki[i-1][j]=stoki[i][j];
        }
    }
    for(int i=0;i<n;i++){
        if(j+1<m and (mapa[i][j+1]==0 or mapa[i][j]+1<mapa[i][j+1]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i][j+1]=mapa[i][j]+1;
            stoki[i][j+1]=stoki[i][j]+1;
        }
        if(i+1<n and (mapa[i+1][j]==0 or mapa[i][j]+1<mapa[i+1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i+1][j]=mapa[i][j]+1;
            stoki[i+1][j]=stoki[i][j]+1;
        }
        if(j-1>=0 and (mapa[i][j-1]==0 or mapa[i][j]+1<mapa[i][j-1]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i][j-1]=mapa[i][j]+1;
            stoki[i][j-1]=stoki[i][j];
        }
        if(i-1>=0 and (mapa[i-1][j]==0 or mapa[i][j]+1<mapa[i-1][j]) and (mapa[i][j]>0 or (i==0 and j==0))){
            mapa[i-1][j]=mapa[i][j]+1;
            stoki[i-1][j]=stoki[i][j];
        }
    }
}
for(int i=0;i<k;i++){
    cin>>a>>b;
    a=a*stoki[n-1][m-1];
    b=b*(mapa[n-1][m-1]-stoki[n-1][m-1]);
    if(najm==(a+b)){
        ile++;
    }
    if(najm>(a+b) or najm==0){
        najm=(a+b);
        ile=1;
    }

}
cout<<najm<<" "<<ile;
return 0;
}