#include <iostream>
#include <bits/stdc++.h>
#include <map>

using namespace std;

int main() {
    int H, W;
    cin >> H >> W;
    char a[H][W];
    int dist[H][W];
    map<char, vector<pair<int, int>>> mp;
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            char c;
            cin >> c;
            a[i][j] = c;
            if(c != '#' && c != '.') mp[c].emplace_back(i, j);
            dist[i][j] = -1;
        }
    }
    dist[mp['S'][0].first][mp['S'][0].second] = 0;
    queue<pair<int, int>> q;
    q.emplace(mp['S'][0].first, mp['S'][0].second);
    // cout << list_d[0].size() << endl;
    vector<int> dx{-1, 0, 1, 0};
    vector<int> dy{0, 1, 0, -1};
    while(q.size()) {
        // cout << "t:" << t << ' ' << e[0] << ' ' << e[1] <<  endl;
        auto [x, y] = q.front();
        q.pop();
        for(int d=0; d<4; d++) {
            // cout << i << ' ' << j << endl;
            int x2 = x + dx[d];
            int y2 = y + dy[d];
            if(dist[x2][y2] == -1 && x2 >= 0 && x2 < H && y2 >= 0 && y2 < W && a[x2][y2] != '#') {
                // cout << x << ' ' << y << ' ' <<  endl;
                if(a[x2][y2] == 'G') {
                    cout << dist[x][y]+1 << endl;
                    return 0;
                }
                dist[x2][y2] = dist[x][y] + 1;
                q.emplace(x2, y2);
            }
        }
        for(const auto& c : mp[a[x][y]]) {
            if(dist[c.first][c.second] >= 0) continue;
            dist[c.first][c.second] = dist[x][y] + 1;
            q.emplace(c.first, c.second);
            // cout << c[0] << ' ' << c[1] << endl;
        }
    }
    cout << -1 << endl;
}
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

ll dp[100][200000 + 10];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, K;
    ll m;
    cin >> n >> K >> m;

    int lim_sum = (n - 1) / 2;
    lim_sum = lim_sum * (lim_sum + 1) / 2 * K;

    dp[0][0] = 1;
    for (int i = 1; i < n; i++) {
        int max_sum = min((i - 1) * i / 2 * K, lim_sum);
        for (int j = 0; j <= max_sum; j++) {
            if (dp[i - 1][j] == 0) continue;
            for (int k = 0; k <= K; k++) {
                if (j + k * i > lim_sum) break;
                dp[i][j + k * i] += dp[i - 1][j];
                if (dp[i][j + k * i] >= m) dp[i][j + k * i] -= m;
            }
        }
    }
    
    for (int i = 1; i <= n; i++) {
        int li = i - 1;
        int ri = n - i;
        int mini = min(li, ri);
        int max_sum = mini * (mini + 1) / 2 * K;

        ll ans = 0;
        for (int j = 0; j <= max_sum; j++) {
            (ans += dp[li][j] * dp[ri][j] % m);
            if (ans >= m) ans -= m;
        }
        ans = (ans * (K + 1) + m - 1) % m;
        cout << ans << newl;
    }
    return 0;
}
