package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
  file, err := os.Open("input")
  if err != nil {
    panic(err)
  }
  defer file.Close()
  scanner := bufio.NewScanner(file)
  resultXMAS := 0
  resultMASX := 0
//  word := []string{"X","M","A","S"}
  table := [][]string{}
  for scanner.Scan() {
    line := scanner.Text()
//    fmt.Println(line)
    table = append(table, strings.Split(line, ""))
  }
  for i := range table {
    for j := range table[i] {
      //XMAS
      if table[i][j] == "X" {
        //HORIZONTAL DOWN
        if i < len(table)-3 && table[i+1][j]+table[i+2][j]+table[i+3][j] == "MAS" {
          resultXMAS++
        }
        //HORIZONTAL UP
        if i >= 3 && table[i-1][j]+table[i-2][j]+table[i-3][j] == "MAS" {
          resultXMAS++
        }
        //HORIZONTAL RIGHT
        if j < len(table[i])-3 && table[i][j+1]+table[i][j+2]+table[i][j+3] == "MAS" {
          resultXMAS++
        }
        //HORIZONTAL LEFT
        if j >= 3 && table[i][j-1]+table[i][j-2]+table[i][j-3] == "MAS" {
          resultXMAS++
        }
        //DIAGONAL DOWNRIGHT
        if i < len(table)-3 && j < len(table[i])-3 && table[i+1][j+1]+table[i+2][j+2]+table[i+3][j+3] == "MAS" {
          resultXMAS++
        }
        //DIAGONAL DOWNLEFT
        if i < len(table)-3 && j >= 3 && table[i+1][j-1]+table[i+2][j-2]+table[i+3][j-3] == "MAS" {
          resultXMAS++
        }
        //DIAGONAL UPLEFT
        if i >= 3 && j >= 3 && table[i-1][j-1]+table[i-2][j-2]+table[i-3][j-3] == "MAS" {
          resultXMAS++
        }
        //DIAGONAL UPRIGHT
        if i >= 3 && j < len(table[i])-3 && table[i-1][j+1]+table[i-2][j+2]+table[i-3][j+3] == "MAS" {
          resultXMAS++
        }
      }
      //MASX
      if table[i][j] == "A" {
        if i < len(table)-1 && i >=1 && j < len(table[i])-1 && j>=1 && (table[i-1][j-1]+table[i+1][j+1] == "MS" || table[i-1][j-1]+table[i+1][j+1] == "SM") && (table[i-1][j+1]+table[i+1][j-1] == "MS" || table[i-1][j+1]+table[i+1][j-1] == "SM") {
          resultMASX++
        }
      }
    }
  }
  fmt.Println("XMAS contained :",resultXMAS)
  fmt.Println("MAS in X contained :",resultMASX)
}
