#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#define rep(i,n) for (int i=0;i<n;i++)

#define DENOM 1000000007

using namespace std;
using ll = long long;

char win_rps(char a, char b) {
  if (a==b) {
    return a ;
  }
  if ((a=='R' && b=='S') || (a=='S' && b=='R')) {
    return 'R' ;
  } else if ((a=='S' && b=='P') || (a=='P' && b=='S')) {
    return 'S' ;
  } else {
    return 'P' ;
  }
}

int main(){
  int n, k ;
  string prev_s, s;
  size_t s_length;
  cin >> n >> k ;
  cin >> prev_s;
  for (int i = 0 ; i< k ; ++i) {
    prev_s += prev_s ;
    s_length = prev_s.size() ;
    if (s_length == 1) {
      cout << prev_s[0] << endl ;
      return 0 ;
    }
    s = "" ;
    for (int j = 0 ; j < s_length ; j+=2) {
      s += win_rps(prev_s[j], prev_s[j+1]) ;
    }
    prev_s = s ;
    cerr << i << ": " << prev_s << endl ;    
  }
  cout << prev_s[0] << endl ;
  return 0 ;
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

short A[501][501], B[501][501];
int D[1 << 18];

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


	int H, W;
	cin >> H >> W;
	rep(i, H) rep(j, W - 1) cin >> A[i][j];
	rep(i, H - 1) rep(j, W) cin >> B[i][j];
	rep(i, H) rep(j, W - 1) A[i][j] <<= 1;
	rep(i, H - 1) rep(j, W) B[i][j] <<= 1;

	priority_queue<ll, vector<ll>, greater<ll>> q;
	D[0] = -2e9 + 1;
	q.push((ll)D[0] << 32);
	int owa = (H - 1) << 9 | W - 1;

	while (q.size()) {
		auto tmp = q.top();
		q.pop();
		int d0 = tmp >> 32;
		int hwp = tmp & (1 << 20) - 1;
		if (D[hwp] != d0) continue;

		int h = hwp >> 9;
		int w = hwp & 511;
		int p = (d0 & 1) << 1 | 1;
		d0 |= 1;

		if (hwp == owa) break;

		if (w > 0) if (D[hwp - 1] > d0 + A[h][w - 1]) {
			D[hwp - 1] = d0 + A[h][w - 1];
			q.push((ll)(d0 + A[h][w - 1]) << 32 | hwp - 1);
		}
		if (w < W - 1) if (D[hwp + 1] > d0 + A[h][w]) {
			D[hwp + 1] = d0 + A[h][w];
			q.push((ll)(d0 + A[h][w]) << 32 | hwp + 1);
		}
		if (h > 0) if (D[hwp - 512] > d0 + p) {
			D[hwp - 512] = d0 + p;
			q.push((ll)(d0 + p) << 32 | hwp - 512);
		}
		if (h < H - 1) if (D[hwp + 512] > d0 + B[h][w]) {
			D[hwp + 512] = d0 + B[h][w];
			q.push((ll)(d0 + B[h][w]) << 32 | hwp + 512);
		}
	}

	co(D[owa] - D[0] >> 1);


	Would you please return 0;
}