code optimazation 6x6 Matrix
You can use arrays to hold sets of row pin numbers or column pin numbers, which then allows use of for loops for pinMode() settings and for testing LEDs one after another, as shown in code below.  

Note, using enum { ... }; for integer-constant declarations is slightly more compact than a series of const int statements.  When a constant name in an enum does not have an = and value after it, it is given a value one larger than the previous constant.  So, for example, the first enum in the following code sets x2 to 3, x3 to 4, etc.

enum { x1=2, x2, x3, x4, x5, x6};
enum { y1=8, y2, y3, y4, y5, y6};
const byte rowPins[] = { x1, x2, x3, x4, x5, x6};
const byte colPins[] = { y1, y2, y3, y4, y5, y6};
enum { nRow = sizeof rowPins / sizeof rowPins[0] };
enum { nCol = sizeof colPins / sizeof colPins[0] };

void setup() {
  for (byte i=0; i&lt;nRow; ++i) pinMode(rowPins[i], OUTPUT);
  for (byte i=0; i&lt;nCol; ++i) pinMode(colPins[i], OUTPUT);
  for (byte i=14; i&lt;18; ++i)  pinMode(i, INPUT);
}

void loop() {
  byte test = digitalRead(17);
  if (test) {
    for (byte x=0; x&lt;nRow; ++x) {
      digitalWrite(rowPins[x], HIGH);
      for (byte y=0; y&lt;nCol; ++y) {
        digitalWrite(colPins[y], HIGH);
        delay(15);
        digitalWrite(colPins[y], LOW);
      }
      digitalWrite(rowPins[x], LOW);
    }
  }
}


Note, subroutines and functions can be used to modularize code and (typically) reduce its volume.  See code sample below.  In the code as is, the subroutine doesn't particularly reduce volume, but definitely adds modularity.  You may find it useful to look up refactoring.

enum { x1=2, x2, x3, x4, x5, x6};
enum { y1=8, y2, y3, y4, y5, y6};
const byte rowPins[] = { x1, x2, x3, x4, x5, x6};
const byte colPins[] = { y1, y2, y3, y4, y5, y6};
enum { nRow = sizeof rowPins / sizeof rowPins[0] };
enum { nCol = sizeof colPins / sizeof colPins[0] };

void setup() {
  for (byte i=0; i&lt;nRow; ++i) pinMode(rowPins[i], OUTPUT);
  for (byte i=0; i&lt;nCol; ++i) pinMode(colPins[i], OUTPUT);
  for (byte i=14; i&lt;18; ++i)  pinMode(i, INPUT);
}

void lightup(byte x, byte y, unsigned int howLong) {
  digitalWrite(rowPins[x], HIGH);
  digitalWrite(colPins[y], HIGH);
  delay(howLong);
  digitalWrite(colPins[y], LOW);
  digitalWrite(rowPins[x], LOW);
}

void loop() {
  byte test = digitalRead(17);
  if (test) {
    for (byte x=0; x&lt;nRow; ++x)
      for (byte y=0; y&lt;nCol; ++y)
        lightup(x, y, 15);
  }
}