7 7
6C 8C KH 8S 2H 7S JH
3H QC QD TS 4C QH J2
7C 6S 6D 8D 7D 4S JS
3S 3D 9S 3C J1 2D 7H
AH KD AS 9C 2S 5D QS
2C JD 8H 9D 4D 5H 5C
JC KC 6H TH TD KS TC
 7 7
2C 3C 4C 2D 3D 4D JC
5C 6C 7C 5D 6D 7D QC
8C 9C TC 8D J2 TD KC
2H 3H 4H J1 3S 4S AC
5H 6H 7H 5S 6S 7S KH
8H 9H TH 8S 9S TS AH
JD QD KD AD JH QH AS
 3 17
3H AS 2S 9C JH JD JC QH 2C QD JS TD 5C QS 4D 5S 8D
6C QC TC KS 6D 7D AH 7C 4C 5H KD 7S 3D 4H 8S 3S KH
J2 2H 9H AD 8C 8H 4S 9D TS 6S 7H 6H 9S AC 2D 3C J1
 7 7
2C J2 5D 3C AH 6S 8H
TD 7S QD TC AD 5C 3H
3D 4H 6C 2S AC AS 7H
JS 9H JD 6D 7C 9S KS
6H 9C QC 8S 9D 4D QS
4S 5H 5S 8D JH 8C 2D
KC 4C TS TH 3S 7D QH
 7 7
QS 5C 2H JC 6C TD 2S
JD 9D 9S 7S 3H 9H 4D
AD 5H AS JH KC 5D QH
3S 8S 8H 4H AC 6D TC
2C KH TH 3C 7C JS 8C
4C 6S QC 6H 3D 7H KD
7D AH 8D TS QD 2D 4S
 6 6
J1 2D 3D 3C QH KH
4D 5D 6D AH TS QS
7D 8D JD KS AS 9S
TD 5C QD 9H TC QC
TH AD 9C KC AC 2C
KD 4C 9D 6C 7C 8C
 7 3
3H J2 4H
5H 6H 7H
8H 9H TH
2S 3S TS
2D 3D 4D
5D 6D 7D
AD KD QD
 3 3
9C TH JS
AH J1 AD
3D 4D 9D
 3 6
2H 3H 4H 5H 2S 3S
6H 7H 8H 9H 4S 5S
TH JH QH KH 6S 7S
 17 3
TH QD TC
AD 6C TS
KS 7H KC
9H 3S 2S
4D 3D 5H
4C 4S 9S
QH 2C KH
6H 8H JH
J1 5C KD
9D J2 JS
7D 9C JD
TD QS 2D
AS 3C QC
5D 8D 8S
3H 7C 8C
5S JC 2H
4H 7S 6D
 5 5
KH J2 JH 8C 5H
7S KC AC 2S KD
7H TS 6D 5C 8H
QD 2H JC QH 3S
6H TD 4H J1 8S
 3 3
4C 2C 3H
8D 8C 5S
AC 7H QH
 3 14
KH 8H 2H 2C 5C AH 2D 5D 8D 2S 5S KC 8S 5H
QH 9H 3H 3C 6C AC 3D 6D AD 3S 6S 9S JS 6H
JH TH 4H 4C 7C TC 4D 7D AS 4S 7S KS QS 7H
 7 7
2C 3C 4C 2D 3D 4D JC
5C 6C J2 5D 6D 7D QC
8C 9C TC 8D 9D TD KC
2H 3H 4H 2S 3S 4S AC
5H 6H 7H 5S 6S 7S KH
8H 9H TH 8S 9S TS AH
JD QD KD AD JH QH AS
 7 7
2C 3C 4C 2D 3D 4D JC
5C 6C 7C 5D 6D 7D QC
8C 9C TC 8D 9D TD KC
2H 3H 4H 2S 3S 4S AC
5H 6H 7H 5S 6S 7S KH
8H 9H TH 8S 9S TS AH
JD QD KD AD JH QH AS
 13 3
TH 9H 8H
7H 6H 5H
JH 3H 4H
2C 3C 4C
AH 6C 7C
8C 9C AS
2D 3D 4D
5D 6D AD
AC 9D KD
2S 3S 4S
KS 6S 7S
8S 9S 2H
JS QS KH
 5 8
2C 2D 2S 2H 3C 3D 3S 3H
4C 4D 4S 4H 5C 5D 5S 5H
8C 9C TC JC 7D 6H 9H J1
KC QC AC AS 7S JS TH QH
6C 7C J2 JH 6S 8H KH AH
 14 3
8H 9H TH
JH QH KH
2H 3H 4H
2C 3C 4C
5C 6C 7C
AC AD TC
2D 3D 4D
5D 6D 7D
8D AH AS
2S 3S 4S
5S 6S 7S
KC 9S KD
JC QC 8S
5H 6H 7H
 9 3
8D AH TD
5D 6D 7D
AS 3D 4D
8C AD TC
5C 6C KH
2C KS 4C
2H 3H 4H
5H 6H 7H
8H 9H TH
 14 3
8H 9H TH
5H 6H 7H
2H 3H 4H
2C 3C 4C
5C AH 7C
8C 9C AC
2D 3D 4D
5D AS 7D
AD KH TD
2S 3S 4S
5S 6S KC
8S KS TS
9S 7S JS
KD QS 6C
 6 3
7C 8C 9C
TC 2C 3C
QC 5C J1
2D 3D 4D
5D J2 6D
7D AD KS
 9 3
2H 4S AC
9S 2D 2S
AD TD 7S
J1 7D TS
3H QH 6H
JH 8S 5C
TC 6C 4H
9D 5D 9H
3S J2 QD
 6 6
3D 7C 8H 2S 5H 6D
QS KH TS 4D TH AH
4H J2 JC 6S QC JS
QH 9C AS 9H AC 4C
2H 3S KC J1 AD 9D
JD 8C 5S 3H JH 7H
 4 4
7S JD 5C 3S
9H 5H AS J2
KD JS 6D 9S
2S 8D JH J1
 6 3
J2 6D 2D
5D J1 7D
8D 9D TD
5H 6C 7C
8H TS QS
KS AS 2H
 6 7
2H 2D 3H 3D J2 2S 3S
4H 4D 5H 5D 4S 5S 6S
6H 6D TH TD 7S 8S 9S
JH JD QH QD AC KC QC
AH AD TS JS JC TC 9C
KH KD QS KS 8C 7C J1
 5 8
2C 2D 2S 2H 3C 3D 3S 3H
4C 4D 4S 4H 5C 5D 5S 5H
6H 9H J2 7D JC 8C 9C TC
JS TH QH 7S AS KC QC AC
8H KH AH 6S JH 6C 7C J1
 3 3
AH 5H J2
4H 8H KC
4C 9C TH
 5 8
2C 2D 2S 2H 3C 3D 3S 3H
4C 4D 4S 4H 5C 5D 5S 5H
6H 9H J1 7D JC 8C 9C TC
JS TH QH 7S AS KC QC AC
8H KH AH 6S JH 6C 7C J2
 4 13
8D 4S QH AS 3D 8C 6H AD 7D 9S 8S KH 7S
KS 2D 2C JH AC 8H 7H 3C J2 9C TS J1 QD
6D 3H QC 4D JD TH JC TD 4H 2H TC 5H KC
5S JS KD 5D AH 5C 4C 3S 6C 9D QS 6S 2S
 3 3
AH 9D 5D
JH 3S QH
J2 2S J1
 3 3
J1 7H 8H
AH QH 9H
J2 KH 2H
 6 7
J1 2H 3H 2D TS TD TC
4H 5H 6H JH JS JD JC
7H 8H 9H KH KS KD KC
7C 8C 9C AH AS AD TH
4C 5C 6C 3S 4S 5S 6S
J2 2C 3C QH QS QD QC
 5 8
2C 2D 2S 2H 3C 3D 3S 3H
4C 4D 4S 4H 5C 5D 5S 5H
8C 9C TC JC 7D 6H 9H J2
KC QC AC AS 7S JS TH QH
6C 7C J1 JH 6S 8H KH AH
 6 7
J1 2H 3H JH JS JC JD
4H 5H 6H 6D TH TS TD
7H 8H 9H 9D QS QC QD
7C 8C 9C KH KS KC KD
4C 5C 6C AH AS AC AD
2C 3C TC QH 6S 7S 8S
 7 7
J2 3C 4C 2D 3D 4D JC
5C 6C 7C 5D 6D 7D QC
8C 9C TC 8D 9D TD KC
2H 3H 4H 2S 3S 4S AC
5H 6H 7H 5S 6S 7S KH
8H 9H TH 8S 9S TS AH
JD QD KD AD JH QH J1
 7 7
4C 5S 6C 3H QD 8D 5C
7C 4D KC 2C 4S TH 2D
5H QS 9D 6S 2H 8H AC
J1 JC 3D TD J2 8C 7H
3C 5D 9S JD 8S 6D 9C
3S 7D AS KD KS KH AH
QH AD QC TC 2S JH TS
 5 5
2H 2S 2D 3D 4D
3H 3S 5D 6D 7D
2C 3C J2 8D 9D
4C 5C 6C 9H 9S
7C 8C 9C 8H 8S
 3 9
TH 2C AC 5H AH 5D 2D 3S TC
JD QC J1 2H 6C 9D 5S 2S 6S
8D KD J2 3D AS KC TS JC 5C
 13 4
AH TC J2 4D
QD 4S 3C KS
6S 8S 5S 9H
2D JS 7H JD
KD QS JC 6C
AD 3S 8C 7D
AS 2S 9C KC
3D J1 9D 8D
4H 7C TH 4C
9S QC QH 2C
2H 6H KH TD
3H 8H 5C TS
AC 5D 5H JH
 7 7
2C 3C 4C 2D 3D 4D JC
5C 6C 7C 5D 6D 7D QC
8C 9C TC 8D 9D TD KC
2H 3H J1 2S 3S 4S AC
5H 6H 7H 5S 6S 7S KH
8H 9H TH 8S 9S TS AH
JD QD KD AD JH QH AS
 7 7
3C 7S 2C AS 4S AC TS
KC 5H 4C 3S JC QS 9H
J1 JS 6H 3D KH 8D 2S
7H 2D 2H 3H 5S 5D 9C
6S TD KD 4H 7C QC 8C
9D TH 9S 5C QH AD 4D
8S JD 8H JH AH QD 6D
 6 7
2H 2D 3H 3D J1 2S 3S
4H 4D 5H 5D 4S 5S 6S
6H 6D TH TD 7S 8S 9S
JH JD QH QD AC KC QC
AH AD TS JS JC TC 9C
KH KD QS KS 8C 7C J2
 6 7
J2 2H 3H JH JS JC JD
4H 5H 6H 6D TH TS TD
7H 8H 9H 9D QS QC QD
7C 8C 9C KH KS KC KD
4C 5C 6C AH AS AC AD
2C 3C TC QH 6S 7S 8S

No solution.
 Solution exists.
Replace J1 with 2S and J2 with 9D.
Put the first square to (1, 1).
Put the second square to (1, 4).
 Solution exists.
Replace J1 with 5D and J2 with TH.
Put the first square to (1, 9).
Put the second square to (1, 12).
 No solution.
 Solution exists.
There are no jokers.
Put the first square to (1, 5).
Put the second square to (5, 1).
 No solution.
 Solution exists.
Replace J2 with 2C.
Put the first square to (1, 1).
Put the second square to (5, 1).
 No solution.
 No solution.
 No solution.
 No solution.
 No solution.
 No solution.
 Solution exists.
Replace J2 with 7C.
Put the first square to (1, 1).
Put the second square to (1, 4).
 Solution exists.
There are no jokers.
Put the first square to (1, 1).
Put the second square to (1, 4).
 No solution.
 Solution exists.
Replace J1 with 7H and J2 with JD.
Put the first square to (3, 1).
Put the second square to (3, 6).
 No solution.
 No solution.
 No solution.
 Solution exists.
Replace J1 with 4C and J2 with 8D.
Put the first square to (1, 1).
Put the second square to (4, 1).
 No solution.
 Solution exists.
Replace J1 with 2C and J2 with 2D.
Put the first square to (1, 1).
Put the second square to (4, 1).
 No solution.
 Solution exists.
Replace J1 with 3C and J2 with 4C.
Put the first square to (1, 1).
Put the second square to (4, 1).
 Solution exists.
Replace J1 with 2C and J2 with AS.
Put the first square to (1, 5).
Put the second square to (4, 5).
 Solution exists.
Replace J1 with JD and J2 with 7H.
Put the first square to (3, 1).
Put the second square to (3, 6).
 No solution.
 Solution exists.
Replace J1 with 7H and J2 with JD.
Put the first square to (3, 1).
Put the second square to (3, 6).
 No solution.
 No solution.
 No solution.
 No solution.
 Solution exists.
Replace J1 with JD and J2 with 7H.
Put the first square to (3, 1).
Put the second square to (3, 6).
 No solution.
 Solution exists.
Replace J1 with 2C and J2 with JS.
Put the first square to (1, 1).
Put the second square to (1, 4).
 No solution.
 No solution.
 No solution.
 Solution exists.
Replace J1 with 6D and J2 with 7S.
Put the first square to (1, 1).
Put the second square to (5, 2).
 Solution exists.
Replace J1 with 4H.
Put the first square to (1, 1).
Put the second square to (1, 4).
 Solution exists.
Replace J1 with 6C.
Put the first square to (1, 5).
Put the second square to (5, 3).
 Solution exists.
Replace J1 with AS and J2 with 2C.
Put the first square to (1, 5).
Put the second square to (4, 5).
 No solution.

#include <bits/stdc++.h>
using namespace std;
struct card {
  int value = -1;
  int suit = -1;
};
bool eqv(card c1, card c2) {
  return (c1.value == c2.value && c1.suit == c2.suit);
}
struct JOKER {
  bool is = false;
  int r;
  int c;
};
JOKER J1, J2;
struct sol {
  int r;
  int c;
  int type_;
  vector<card> repl1;
  vector<card> repl2;
};
bool if_J_in(int r, int c, int N) {
  if (N == 1 && J1.is && J1.r >= r && J1.r <= r + 2 && J1.c >= c &&
      J1.c <= c + 2)
    return true;
  if (N == 2 && J2.is && J2.r >= r && J2.r <= r + 2 && J2.c >= c &&
      J2.c <= c + 2)
    return true;
  return false;
}
bool ch_dif(int n, int m, vector<card> &On_Table, int r, int c) {
  for (int i = 0; i < 9; ++i) {
    int r1 = i / 3;
    int c1 = i - 3 * r1;
    r1 += r;
    c1 += c;
    int v1 = On_Table[r1 * m + c1].value;
    if (v1 < 0) continue;
    for (int j = i + 1; j < 9; ++j) {
      int r2 = j / 3;
      int c2 = j - 3 * r2;
      r2 += r;
      c2 += c;
      int v2 = On_Table[r2 * m + c2].value;
      if (v1 == v2 && v1 >= 0 && v2 >= 0) return false;
    }
  }
  return true;
}
void def_val(bool *values, int n, int m, vector<card> &On_Table, int r, int c) {
  for (int i = 0; i < 9; ++i) {
    int r1 = i / 3;
    int c1 = i - 3 * r1;
    r1 += r;
    c1 += c;
    int v1 = On_Table[r1 * m + c1].value;
    if (v1 >= 0) values[v1] = false;
  }
}
void if_sol(int r, int c, bool *cards, vector<card> &On_Table,
            vector<sol> &Solutions, int n, int m) {
  if (!if_J_in(r, c, 1) && !if_J_in(r, c, 2)) {
    if (ch_dif(n, m, On_Table, r, c)) {
      sol tmp;
      tmp.c = c;
      tmp.r = r;
      tmp.type_ = 0;
      Solutions.push_back(tmp);
    }
    return;
  }
  if (if_J_in(r, c, 1) || if_J_in(r, c, 2)) {
    if (ch_dif(n, m, On_Table, r, c)) {
      sol tmp;
      tmp.c = c;
      tmp.r = r;
      if (if_J_in(r, c, 1) && !if_J_in(r, c, 2)) tmp.type_ = 1;
      if (!if_J_in(r, c, 1) && if_J_in(r, c, 2)) tmp.type_ = 2;
      if (if_J_in(r, c, 1) && if_J_in(r, c, 2)) tmp.type_ = 3;
      bool values[13];
      for (int i = 0; i < 13; ++i) values[i] = true;
      def_val(values, n, m, On_Table, r, c);
      for (int v = 0; v < 13; ++v) {
        if (!values[v]) continue;
        for (int s = 0; s < 4; ++s) {
          if (cards[s * 13 + v]) {
            card tmp_;
            tmp_.suit = s;
            tmp_.value = v;
            tmp.repl1.push_back(tmp_);
            tmp.repl2.push_back(tmp_);
          }
        }
      }
      if (tmp.type_ == 1 && tmp.repl1.size() > 0) Solutions.push_back(tmp);
      if (tmp.type_ == 2 && tmp.repl2.size() > 0) Solutions.push_back(tmp);
      if (tmp.type_ == 3 && tmp.repl1.size() > 1) Solutions.push_back(tmp);
    }
    return;
  }
  return;
}
void print_card(card a) {
  if (a.value == -1) cout << "J" << a.suit;
  if (a.value >= 0) {
    switch (a.value) {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        cout << a.value + 2;
        break;
      case 8:
        cout << "T";
        break;
      case 9:
        cout << "J";
        break;
      case 10:
        cout << "Q";
        break;
      case 11:
        cout << "K";
        break;
      case 12:
        cout << "A";
        break;
    }
    switch (a.suit) {
      case 0:
        cout << "C";
        break;
      case 1:
        cout << "D";
        break;
      case 2:
        cout << "H";
        break;
      case 3:
        cout << "S";
        break;
    }
  }
}
void print_sols(vector<sol> &S) {
  for (int i = 0; i < S.size(); ++i) {
    cout << S[i].r << " " << S[i].c << " " << S[i].type_ << endl;
    if (S[i].type_ == 1 || S[i].type_ == 3) {
      for (int j = 0; j < S[i].repl1.size(); ++j) print_card(S[i].repl1[j]);
      cout << endl;
    }
    if (S[i].type_ == 2 || S[i].type_ == 3)
      for (int j = 0; j < S[i].repl2.size(); ++j) print_card(S[i].repl2[j]);
    cout << endl;
  }
  cout << endl;
}
bool both(sol s1, sol s2, bool *cards) {
  if (fabs(s1.r - s2.r) < 3 && fabs(s1.c - s2.c) < 3) return false;
  if (s1.type_ == 0 && s2.type_ == 0) {
    cout << "Solution exists." << endl;
    if (!J1.is && !J2.is) cout << "There are no jokers." << endl;
    if (J1.is && !J2.is) {
      int i = 0;
      for (i = 0; i < 13 * 4; ++i)
        if (cards[i]) break;
      if (i >= 13 * 4) return false;
      card tmp;
      tmp.suit = i / 13;
      tmp.value = i - tmp.suit * 13;
      cout << "Replace J1 with ";
      print_card(tmp);
      cout << "." << endl;
    }
    if (!J1.is && J2.is) {
      int i = 0;
      for (i = 0; i < 13 * 4; ++i)
        if (cards[i]) break;
      if (i >= 13 * 4) return false;
      card tmp;
      tmp.suit = i / 13;
      tmp.value = i - tmp.suit * 13;
      cout << "Replace J2 with ";
      print_card(tmp);
      cout << "." << endl;
    }
    if (J1.is && J2.is) {
      int i = 0;
      for (i = 0; i < 13 * 4; ++i)
        if (cards[i]) break;
      int j = 0;
      for (j = i + 1; j < 13 * 4; ++j)
        if (cards[j]) break;
      if (i >= 13 * 4 || j >= 13 * 4) return false;
      card tmp1;
      tmp1.suit = i / 13;
      tmp1.value = i - tmp1.suit * 13;
      card tmp2;
      tmp2.suit = j / 13;
      tmp2.value = j - tmp2.suit * 13;
      cout << "Replace J1 with ";
      print_card(tmp1);
      cout << " and J2 with ";
      print_card(tmp2);
      cout << "." << endl;
    }
    cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1 << ")."
         << endl;
    cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1 << ")."
         << endl;
    return true;
  }
  if (s1.type_ == 1 && s2.type_ == 2) {
    if (s1.repl1.size() > 1 || s2.repl2.size() > 1 ||
        !eqv(s1.repl1[0], s2.repl2[0])) {
      cout << "Solution exists." << endl;
      card tmp1 = s1.repl1[0];
      card tmp2 = s2.repl2[0];
      if (eqv(s1.repl1[0], s2.repl2[0])) {
        if (s1.repl1.size() > 1)
          tmp1 = s1.repl1[1];
        else
          tmp2 = s2.repl2[2];
      }
      cout << "Replace J1 with ";
      print_card(tmp1);
      cout << " and J2 with ";
      print_card(tmp2);
      cout << "." << endl;
      cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1
           << ")." << endl;
      cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1
           << ")." << endl;
      return true;
    } else
      return false;
  }
  if (s1.type_ == 2 && s2.type_ == 1) {
    if (s1.repl2.size() > 1 || s2.repl1.size() > 1 ||
        !eqv(s1.repl2[0], s2.repl1[0])) {
      cout << "Solution exists." << endl;
      card tmp2 = s1.repl2[0];
      card tmp1 = s2.repl1[0];
      if (eqv(tmp1, tmp2)) {
        if (s1.repl2.size() > 1)
          tmp2 = s1.repl2[1];
        else
          tmp1 = s2.repl1[2];
      }
      cout << "Replace J1 with ";
      print_card(tmp1);
      cout << " and J2 with ";
      print_card(tmp2);
      cout << "." << endl;
      cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1
           << ")." << endl;
      cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1
           << ")." << endl;
      return true;
    } else
      return false;
  }
  if (s1.type_ == 0 && s2.type_ == 1) {
    card tmp;
    if (J2.is) {
      int i;
      for (i = 0; i < 13 * 4; ++i) {
        tmp.suit = i / 13;
        tmp.value = i - tmp.suit * 13;
        if (cards[i] && !eqv(tmp, s2.repl1[0])) break;
      }
      if (i > 13 * 4) return false;
    }
    cout << "Solution exists." << endl;
    cout << "Replace J1 with ";
    print_card(s2.repl1[0]);
    if (J2.is) {
      cout << " and J2 with ";
      print_card(tmp);
    }
    cout << "." << endl;
    cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1 << ")."
         << endl;
    cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1 << ")."
         << endl;
    return true;
  }
  if (s1.type_ == 0 && s2.type_ == 2) {
    card tmp;
    if (J1.is) {
      int i;
      for (i = 0; i < 13 * 4; ++i) {
        tmp.suit = i / 13;
        tmp.value = i - tmp.suit * 13;
        if (cards[i] && !eqv(tmp, s2.repl2[0])) break;
      }
      if (i > 13 * 4) return false;
    }
    cout << "Solution exists." << endl;
    if (J1.is) {
      cout << "Replace J1 with ";
      print_card(tmp);
      cout << " and J2 with ";
    } else {
      cout << "Replace J2 with ";
    }
    print_card(s2.repl2[0]);
    cout << "." << endl;
    cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1 << ")."
         << endl;
    cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1 << ")."
         << endl;
    return true;
  }
  if (s1.type_ == 2 && s2.type_ == 0) {
    card tmp;
    if (J1.is) {
      int i;
      for (i = 0; i < 13 * 4; ++i) {
        tmp.suit = i / 13;
        tmp.value = i - tmp.suit * 13;
        if (cards[i] && !eqv(tmp, s1.repl2[0])) break;
      }
      if (i > 13 * 4) return false;
    }
    cout << "Solution exists." << endl;
    if (J1.is) {
      cout << "Replace J1 with ";
      print_card(tmp);
      cout << " and J2 with ";
    } else {
      cout << "Replace J2 with ";
    }
    print_card(s1.repl2[0]);
    cout << "." << endl;
    cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1 << ")."
         << endl;
    cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1 << ")."
         << endl;
    return true;
  }
  if (s1.type_ == 1 && s2.type_ == 0) {
    card tmp;
    if (J2.is) {
      int i;
      for (i = 0; i < 13 * 4; ++i) {
        tmp.suit = i / 13;
        tmp.value = i - tmp.suit * 13;
        if (cards[i] && !eqv(tmp, s1.repl1[0])) break;
      }
      if (i > 13 * 4) return false;
    }
    cout << "Solution exists." << endl;
    cout << "Replace J1 with ";
    print_card(s1.repl1[0]);
    if (J2.is) {
      cout << " and J2 with ";
      print_card(tmp);
    }
    cout << "." << endl;
    cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1 << ")."
         << endl;
    cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1 << ")."
         << endl;
    return true;
  }
  if (s1.type_ == 3) {
    if (s1.repl1.size() > 1) {
      card tmp1, tmp2;
      int i, j;
      bool ok = false;
      for (i = 0; i < s1.repl1.size() && !ok; ++i)
        for (j = i + 1; j < s1.repl1.size(); ++j) {
          if (s1.repl1[i].value != s1.repl1[j].value) {
            tmp1 = s1.repl1[i];
            tmp2 = s1.repl1[j];
            ok = true;
            break;
          }
        }
      if (!ok) return false;
      cout << "Solution exists." << endl;
      cout << "Replace J1 with ";
      print_card(tmp1);
      cout << " and J2 with ";
      print_card(tmp2);
      cout << "." << endl;
      cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1
           << ")." << endl;
      cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1
           << ")." << endl;
      return true;
    }
    return false;
  }
  if (s2.type_ == 3) {
    if (s2.repl1.size() > 1) {
      card tmp1, tmp2;
      int i, j;
      bool ok = false;
      for (i = 0; i < s2.repl1.size() && !ok; ++i)
        for (j = i + 1; j < s2.repl1.size(); ++j) {
          if (s2.repl1[i].value != s2.repl1[j].value) {
            tmp1 = s2.repl1[i];
            tmp2 = s2.repl1[j];
            ok = true;
            break;
          }
        }
      if (!ok) return false;
      cout << "Solution exists." << endl;
      cout << "Replace J1 with ";
      print_card(tmp1);
      cout << " and J2 with ";
      print_card(tmp2);
      cout << "." << endl;
      cout << "Put the first square to (" << s1.r + 1 << ", " << s1.c + 1
           << ")." << endl;
      cout << "Put the second square to (" << s2.r + 1 << ", " << s2.c + 1
           << ")." << endl;
      return true;
    }
    return false;
  }
  return false;
}
int main() {
  bool CARDS[13 * 4];
  for (int i = 0; i < 13; ++i)
    for (int j = 0; j < 4; ++j) CARDS[j * 13 + i] = true;
  int n, m;
  cin >> n >> m;
  vector<card> On_Table(n * m);
  vector<sol> Solutions;
  for (int i = 0; i < n * m; ++i) {
    string tmp;
    cin >> tmp;
    switch (tmp[0]) {
      case 'J':
        if (tmp[1] == '1' || tmp[1] == '2')
          On_Table[i].value = -1;
        else
          On_Table[i].value = 9;
        break;
      case '2':
        On_Table[i].value = 0;
        break;
      case '3':
        On_Table[i].value = 1;
        break;
      case '4':
        On_Table[i].value = 2;
        break;
      case '5':
        On_Table[i].value = 3;
        break;
      case '6':
        On_Table[i].value = 4;
        break;
      case '7':
        On_Table[i].value = 5;
        break;
      case '8':
        On_Table[i].value = 6;
        break;
      case '9':
        On_Table[i].value = 7;
        break;
      case 'T':
        On_Table[i].value = 8;
        break;
      case 'Q':
        On_Table[i].value = 10;
        break;
      case 'K':
        On_Table[i].value = 11;
        break;
      case 'A':
        On_Table[i].value = 12;
        break;
      default:
        cout << "ERROR" << endl;
        return 0;
        break;
    }
    switch (tmp[1]) {
      case '1':
        On_Table[i].suit = 1;
        J1.is = true;
        J1.r = i / m;
        J1.c = i - J1.r * m;
        break;
      case '2':
        On_Table[i].suit = 2;
        J2.is = true;
        J2.r = i / m;
        J2.c = i - J2.r * m;
        break;
      case 'C':
        On_Table[i].suit = 0;
        break;
      case 'D':
        On_Table[i].suit = 1;
        break;
      case 'H':
        On_Table[i].suit = 2;
        break;
      case 'S':
        On_Table[i].suit = 3;
        break;
      default:
        cout << "ERROR" << endl;
        return 0;
        break;
    }
  }
  for (int i = 0; i < n * m; ++i) {
    if (On_Table[i].suit >= 0)
      CARDS[On_Table[i].value + 13 * On_Table[i].suit] = false;
  }
  for (int r = 0; r < n - 2; ++r)
    for (int c = 0; c < m - 2; ++c)
      if_sol(r, c, CARDS, On_Table, Solutions, n, m);
  for (int i = 0; i < Solutions.size(); ++i)
    for (int j = i + 1; j < Solutions.size(); ++j)
      if (both(Solutions[i], Solutions[j], CARDS)) return 0;
  cout << "No solution." << endl;
  return 0;
}
